All for Joomla All for Webmasters

AngularJS Directives

AngularJS facilitates you to extend HTML with new attributes. These attributes are called directives.

There is a set of built-in directive in AngularJS which offers functionality to your applications. You can also define your own directives.

Directives are special attributes starting with ng- prefix. Following are the most common 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

ng-app directive defines the root element. It starts an AngularJS Application and automatically initializes or bootstraps the application when web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application.

See this example:

In following example, we’ve defined a default AngularJS application using ng-app attribute of a div element.

  1. <div ng-app = “”>
  2.    …
  3. </div>

ng-init directive

ng-init directive initializes an AngularJS Application data. It defines the initial values for an AngularJS application.

In following example, we’ll initialize an array of countries. We’re using JSON syntax to define array of countries.

  1. <div ng-app = “” ng-init = “countries = [{locale:’en-IND’,name:’India’}, {locale:’en-PAK’,name:’Pakistan’}, {locale:’en-AUS’,name:’Australia’}]”>
  2.    …
  3. </div>

ng-model directive:

ng-model directive defines the model/variable to be used in AngularJS Application.

In following example, we’ve defined a model named “name”.

  1. <div ng-app = “”>
  2.    …
  3.    <p>Enter your Name: <input type = “text” ng-model = “name”></p>
  4. </div>

ng-repeat directive

ng-repeat directive repeats html elements for each item in a collection. In following example, we’ve iterated over array of countries.

  1. <div ng-app = “”>
  2.    …
  3.    <p>List of Countries with locale:</p>
  4.    <ol>
  5.       <li ng-repeat = “country in countries”>
  6.          {{ ‘Country: ‘ + country.name + ‘, Locale: ‘ + country.locale }}
  7.       </li>
  8.    </ol>

AngularJS directives Example

Let’s take an example to use all the above discussed directives:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.       <title>AngularJS Directives</title>
  5. </head>
  6. <body>
  7.       <h1>Sample Application</h1>
  8.       <div ng-app = “” ng-init = “countries = [{locale:’en-IND’,name:’India’}, {locale:’en-PAK’,name:’Pakistan’}, {locale:’en-AUS’,name:’Australia’}]”>
  9.          <p>Enter your Name: <input type = “text” ng-model = “name”></p>
  10.          <p>Hello <span ng-bind = “name”></span>!</p>
  11.          <p>List of Countries with locale:</p>
  12.          <ol>
  13.             <li ng-repeat = “country in countries”>
  14.                {{ ‘Country: ‘ + country.name + ‘, Locale: ‘ + country.locale }}
  15.             </li>
  16.          </ol>
  17.       </div>
  18. <script src = “http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”></script>
  19. </body>
  20. </html>
PinIt
submit to reddit

Leave a Reply

Top