Angular Directives

Angular Directives

At a high level, Angular directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS’s HTML compiler ($compile) to attach a specified behavior to that DOM element (e.g. via event listeners), or even to transform the DOM element and its children.

AngularJS comes with a set of these directives built-in, like ngBind, ngModel, and ngClass. Much like you create controllers and services, you can create your own directives for AngularJS to use. When AngularJS bootstraps your application, the HTML compiler traverses the DOM matching directives against the DOM elements.
What does it mean to “compile” an HTML template? For AngularJS, “compilation” means attaching directives to the HTML to make it interactive. The reason we use the term “compile” is that the recursive process of attaching directives mirrors the process of compiling source code in compiled programming languages.

 

Matching Angular Directives

Before we can write a directive, we need to know how AngularJS’s HTML compiler determines when to use a given directive.

Similar to the terminology used when an element matches a selector, we say an element matches a directive when the directive is part of its declaration.

In the following example, we say that the <input> element matches the ngModel directive

<input ng-model=”foo”>

The following <input> element also matches ngModel:

<input data-ng-model=”foo”>

And the following <person> element matches the person directive:

<person>{{name}}</person>

IT Professionals, Web Developers, web programmers, IT students can use the below links to be updated on Angular 4

Angular 4 Tutorial IndexBack to Angular 4 Tutorial Main Page

Angular Libraries
Angular Directive Basics

Get industry recognized certification – Contact us

keyboard_arrow_up