Book Image

AngularJS Directives Cookbook

By : Fernando Monteiro, Jan Carlo Viray, Dror Elovits, Juri Strumpflohner, Gabriel Schenker
Book Image

AngularJS Directives Cookbook

By: Fernando Monteiro, Jan Carlo Viray, Dror Elovits, Juri Strumpflohner, Gabriel Schenker

Overview of this book

AngularJS directives are at the center of what makes it such an exciting – and important - web development framework. With directives, you can take greater control over HTML elements on your web pages – they ‘direct’ Angular’s HTML compiler to behave in the way you want it to. It makes building modern web applications a much more expressive experience, and allows you to focus more closely on improving the way that user interaction impacts the DOM and the way your app manages data. If you’re already using Angular, you probably recognize the power of directives to transform the way you understand and build your projects – but customizing and creating your own directives to harness AngularJS to its full potential can be more challenging. This cookbook shows you how to do just that – it’s a valuable resource that demonstrates how to use directives at every stage in the workflow. Packed with an extensive range of solutions and tips that AngularJS developers shouldn’t do without, you’ll find out how to make the most of directives. You’ll find recipes demonstrating how to build a number of different user interface components with directives, so you can take complete control over how users interact with your application. You’ll also learn how directives can simplify the way you work by creating reusable directives – by customizing them with Yeoman you can be confident that you’re application has the robust architecture that forms the bedrock of the best user experiences. You’ll also find recipes that will help you learn how to unit test directives, so you can be confident in the reliability and performance of your application. Whether you’re looking for guidance to dive deeper into AngularJS directives, or you want a reliable resource, relevant to today’s web development challenges, AngularJS Directives Cookbook delivers everything you need in an easily accessible way.
Table of Contents (16 chapters)
AngularJS Directives Cookbook
About the Author
About the Reviewers

Using dynamic templates on directives

In this first example we will create a directive using inline templates rendered by the JavaScript inside of the directive. Also, in the example we will simulate the $http.get() method to get the closest example of a real situation.

In addition, we will see an important tip for loading videos from external sources using our application on a local server.

Getting ready

We are still using the Yeoman generator-angm (version 0.2.6) to build the base line application. You can get the code in the examples folder at PacktPub, or open your terminal window and type:

yo angm

Name it dynamic-templates and press Enter, Enter, and Enter..


The generator-angm (version 0.2.6) uses the new stable version from AngularJS, 1.4.5. For this chapter, it is very important to use the same version because on the next release, the generator will have some major changes and will use a different approach to build AngularJS modular applications.

How to do it…

  1. Before we start to code...