AngularJS - Directives



AngularJS directives are used to extend HTML. They are special attributes starting with ng-prefix. Let us discuss the following directives −

  • ng-app − This directive starts an AngularJS Application.

  • ng-init − This directive initializes application data.

  • ng-model − This directive defines the model that is variable to be used in AngularJS.

  • ng-repeat − This directive repeats HTML elements for each item in a collection.

ng-app directive

The ng-app directive starts an AngularJS Application. It defines the root element. It automatically initializes or bootstraps the application when the web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application. In the following example, we define a default AngularJS application using ng-app attribute of a

element.

...

ng-init directive

The ng-init directive initializes an AngularJS Application data. It is used to assign values to the variables. In the following example, we initialize an array of countries. We use JSON syntax to define the array of countries.

...

ng-model directive

The ng-model directive defines the model/variable to be used in AngularJS Application. In the following example, we define a model named name.

...

Enter your Name:

ng-repeat directive

The ng-repeat directive repeats HTML elements for each item in a collection. In the following example, we iterate over the array of countries.

...

List of Countries with locale:

  1. {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}

Example

The following example shows the use of all the above-mentioned directives.

testAngularJS.htm


   
      AngularJS Directives
   
   
   
      

Sample Application

Enter your Name:

Hello !

List of Countries with locale:

  1. {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}

Output

Open the file testAngularJS.htm in a web browser. Enter your name and see the result.

Advertisements