Runoob Tutorial ArchiveEnglish

AngularJS Directives

AngularJS Directives


AngularJS extends HTML through new attributes calledDirectivesAngularJS adds functionality to applications through built-in directives.

AngularJS adds functionality to applications through built-in directives.

AngularJS allows you to define custom directives.


AngularJS directives

AngularJS directives are extended HTML attributes with a prefixng-.

ng-appThe directive initializes an AngularJS application.

ng-initInitializes application data for the directive.

ng-modelThe directive binds the element's value (such as the value of an input field) to the application.

The complete directive content can be found in AngularJS Reference Guide.

AngularJS instance

<

div

ng-app

=

"

"

ng-init

=

"

firstName='John'

"

<

p

Try entering in the input box:

</

p

<

p

Name:

<

input

type

=

"

text

"

ng-model

=

"

firstName

"

</

p

<

p

You entered: {{ firstName }}

</

p

</

div

Try it »

ng-apptells AngularJS that the

element is AngularJSApplication"owner" of

Data binding

in the instance{{ firstName }}The expression is an AngularJS data binding expression.

Data binding in AngularJS synchronizes AngularJS expressions with AngularJS data.

{{ firstName }}is done throughng-model="firstName"synchronization.

In the next instance, the two text fields are synchronized through two ng-model directives:

AngularJS instance

<

div

ng-app

=

"

"

ng-init

=

"

quantity=1;price=5

"

<

h2

Price Calculator

</

h2

Quantity:

<

input

type

=

"

number

"

ng-model

=

"

quantity

"

Price:

<

input

type

=

"

number

"

ng-model

=

"

price

"

<

p

<

b

Total price:

</

b

{{ quantity * price }}

</

p

</

div

Try it »

Note Useng-initIt is not very common. You will learn a better way to initialize data in the controller chapter.

Repeat HTML element

ng-repeatThe directive repeats an HTML element:

AngularJS instance

<

div

ng-app

=

"

"

ng-init

=

"

names=['Jani','Hege','Kai']

"

<

p

Use ng-repeat to loop through an array

</

p

<

ul

<

li

ng-repeat

=

"

x in names

"

{{ x }}

</

li

</

ul

</

div

Try it »

ng-repeatThe directive is used on an object array:

AngularJS instance

<

div

ng-app

=

"

"

ng-init

=

"

names=[ {name:'Jani',country:'Norway'}, {name:'Hege',country:'Sweden'}, {name:'Kai',country:'Denmark'}]

"

<

p

Loop object:

</

p

<

ul

<

li

ng-repeat

=

"

x in names

"

{{ x.name + ', ' + x.country }}

</

li

</

ul

</

div

Try it »

|NoteAngularJS perfectly supports CRUD (Create, Read, Update, Delete) applications with a database.
Imagine the objects in the instance as records in the database. | | --- | --- |


ng-app directive

ng-appThe directive defines theroot element of the AngularJS application..

ng-appThe directive willautomatically guide(automatically initialize) the application when the webpage has loaded.

Later you will learnhow ng-appconnects to a code module through a value (e.g., ng-app="myModule").


ng-init directive

ng-initdefines theinitial valuefor an AngularJS application.

Usually, ng-init is not used. You will use a controller or a module instead.

You will learn more about controllers and modules later.


ng-model directive

ng-modelDirectivesBind HTML elementsto application data.

ng-modelDirectives can also:

  • provide type validation for application data (number, email, required).
  • provide state for application data (invalid, dirty, touched, error).
  • provide CSS classes for HTML elements.
  • Bind an HTML element to an HTML form.

ng-repeat directive

ng-repeatThe directive willclone an HTML element for each item in the collection (array)..


Create custom directives

In addition to AngularJS built-in directives, we can also create custom directives.

You can use.directivefunction to add custom directives.

To call a custom directive, you need to add the custom directive name to the HTML element.

Name a directive using camelCase, ** runoobDirective**, but when using it, you need to use-separator,runoob-directive:

AngularJS instance

<

body

ng-app=

"myApp"

<

runoob-directive

<

/runoob-directive

<

script

var app = angular.module("myApp", []);

app.directive("runoobDirective", function() {

    return {

        template : "

Custom instructions!

"

};

});

<

/script

<

/body

Try it »

You can call the command in the following ways:

  • Element name
  • Attribute
  • Class name
  • Comment

The following instance methods can also output the same result:

Element name

<

runoob-directive

<

/runoob-directive

Try it »

Attribute

<

div

runoob-directive

<

/div

Try it »

Class name

<

div

class=

"runoob-directive"

<

/div

Try it »

Comment

Try it »


Usage limit

You can restrict your instructions to be called only through specific ways.

Instance

By addingrestrictattribute and setting its value to"A", to set that the instruction can only be called through the attribute:

var

app = angular.module(

"myApp"

, []);

app.directive(

"runoobDirective"

,

function

() {

   

return

{

restrict :

"A"

,

template :

"

Custom instruction!

"

};

});

Try it »

restrictThe value can be one of the following:

  • EUsed as an element name
  • AUsed as an attribute
  • CUsed as a class name
  • MUsed as a comment

restrictThe default value isEA, meaning the command can be called by element name and attribute name.