We now have a youtube channel. Subscribe!

AngularJS | Directives

AngularJS Directives


Hello folks! welcome back to a new section of our tutorial on AngularJS. In this tutorial, we're going to be studying about AngularJS Directives.

AngularJS directives are used for extending HTML. They're special attributes beginning with ng-prefix. Let us discuss the following directives -

  • ng-app - The ng-app directive starts an AngularJS Application.
  • ng-init - The ng-init directive initializes application data.
  • ng-model - ng-model directive defines the model that is variable to be used in AngularJS.
  • ng-repeat - ng-repeat directive repeats the HTML elements for each item in a collection.

ng-app Directive

The ng-app starts an AngularJS application. It defines the root element. It automatically initializes the application when the webpage containing AngularJS Application is loaded. It's also used for loading various AngularJS modules in AngularJS Application.

Example

In the following example below, we define a default AngularJS application using np-app attribute of a <div> element.

<div ng-app = "">
   ...
</div>

ng-init Directive

The ng-init directive initializes an AngularJS Application data. It is used to assign values to the variables.

Example

In following example below, we initialize an array of countries. We used JSON syntax to define the array of countries.

<div ng-app = "" ng-init = "countries = [{locale:'en-US',name:'United States'}, 
   {locale:'en-GB',name:'United Kingdom'}, {locale:'en-FR',name:'France'}]">
   ...
</div>


ng-model Directive

The ng-model directive defines the variables to be used in AngularJS Application.

Example

In the following example, we define a model named name.

<div ng-app = "">
   ...
   <p>Enter your Name: <input type = "text" ng-model = "name"></p>
</div>

ng-repeat Directive

The ng-repeat repeats HTML elements for each item in a collection.

Example

In the following example below, we iterate over the array of countries.

<div ng-app = "">
   ...
   <p>List of Countries with locale:</p>
   
   <ol>
      <li ng-repeat = "country in countries">
         {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}
      </li>
   </ol>
</div>

Example

The following example illustrates the use of all the above mentioned directives.

Create a HTML file testAngularJS.html -

<html>
   <head>
      <title>AngularJS Directives</title>
   </head>
   
   <body>
      <h1>Sample Application</h1>
      
      <div ng-app = "" ng-init = "countries = [{locale:'en-US',name:'United States'}, 
         {locale:'en-GB',name:'United Kingdom'}, {locale:'en-FR',name:'France'}]"> 
         <p>Enter your Name: <input type = "text" ng-model = "name"></p>
         <p>Hello <span ng-bind = "name"></span>!</p>
         <p>List of Countries with locale:</p>
      
         <ol>
            <li ng-repeat = "country in countries">
               {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}
            </li>
         </ol>
      </div>
      
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
      </script>
      
   </body>
</html>

Output

Open the file testAngularJS.html in any web browser of your choice and input your name to see the result -

Sample Application

Enter your Name: 

Hello !

List of Countries with locale:

  1. Country: United States, Locale: en-US
  2. Country: United Kingdom, Locale: en-GB
  3. Country: France, Locale: en-FR


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about AngularJS Expressions.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain