Description ng-if directive and the ng-repeat directive are some of the examples of directives. For which custom directive is activated it replaces the element. While bootstrapping, AngularJS application searches for the matching elements and complies using compile() method of the custom directive and then uses link() method to process the element. The chapter explains when to create custom directives and how to execute them.
Description
Directives | Directive Type | Description |
|---|---|
| Element directive | At the point when AngularJS finds a coordinating HTML element in the HTML template, Element directive is enacted. |
| Attribute directive | Attribute directive gets enacted when AngularJS finds a coordinating HTML element attribute. |
| CSS class directive | CSS class directive gets enacted when AngularJS finds a coordinating CSS Class. |
| Comment directive | This directive gets enacted when AngularJS finds a coordinating HTML comment. |
Examples directive() function. The initial parameter of the function call to the directive() is the directive name to register. This is the name which is needed to use in HTML templates whenever there is a need to activate that directive.
In the above example, customer is used as a name i.e. this directive is activated as an HTML element with name customer in the HTML template.
The factory function is the second parameter of the directive function passed. A directive definition has to be returned by this function whenever requested. To get a JavaScript object that contains the definition of the directive, AngularJS will request this function.
In the above example, a JavaScript object is used inside the function .
Description template and restrict field.
The restrict field helps in checking whether the directive has to be activated by matching HTML element or by an element attribute.
E It tells only HTML elements which are named as customer must activate the directive.A It tells only HTML attributes which are named as customer must activate the directive.AE can also be used to match both HTML element names and attribute names.customer element content is a template field. It works when the matched customer element content is not found and instead, this HTML template was located in the similar place.
Examples div element, the activation of added directive will take place. In place of the div element, the HTML will be inserted:
[html]
Name: Smith
[/html]
Description compile() and link() functions help in performing advanced operation inside the directive created.compile() and link() functions characterizes the way of modification if directive in the HTML document matches the directive.compile() function is called once for each occurrence of the directive in the HTML page. The compile() function can then do any one-time configuration required by the element containing the directive.compile() function completes its task by returning the link() function. In this process, link() is called each time whenever the element has to bound to data in the $scope object.compile() function can be summed up with the directive definition object, and link() function is returned by the compile() function when executed.
Key Points