---
title: "AngularJs Directives"  
description: "Hi everyone in this article I’m explaining about Angularjs Directives."  
author: "Anonymous User"  
published: 2015-03-19  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1676/angularjs-directives  
category: "angular js"  
tags: ["javascript", "angularjs directive", "javascript library"]  
reading_time: 2 minutes  

---

# AngularJs Directives

Hi everyone in this article I’m explaining about Angularjs [Directives](https://www.mindstick.com/blog/44/directives-in-asp-dot-net).\

##### Details:

AngularJS directives are used to extend HTML. These are special [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) starting with ng- prefix. We're going to discuss following directives:\

1. ng-app - This directive starts an AngularJS [Application](https://www.mindstick.com/articles/12824/calculator-application-in-android).

2. ng-init - This directive initializes application data.

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

4. ng-repeat - This directive repeats [html elements](https://www.mindstick.com/forum/158688/how-can-you-dynamically-create-html-elements-using-jquery) for each item in a [collection](https://www.mindstick.com/articles/1718/collections-in-java).

##### Ng-app Directive:

ng-app directive starts an AngularJs Application. It defines the [root element](https://www.mindstick.com/interview/1692/what-is-root-element-in-xml). It automatically initializes the application when [web page](https://answers.mindstick.com/qa/92898/how-to-create-a-web-page-using-bootstrap-4) containing Angularjs Application is loaded. It is also used to load various Angularjs modules in angularjs application. In following example we defined a default AngularJs application using ng-app attribute of a [div element](https://www.mindstick.com/forum/34450/remove-div-element).\

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

##### Ng-init directive:

Ng-init directive initializes an AngularJs Application data. It is used to put values to the variables to be used in the application. In following example we’ll [initialize an array](https://www.mindstick.com/forum/159225/how-can-i-declare-and-initialize-an-array-in-java) of countries we are using JSON syntax to define 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:

Ng-model directive defines the model/variable to be used in Angularjs Application. In following example we defined a model named “username”\

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

##### Ng-repeat directive:

Ng-repeat directive repeats html elements for each time in a collection in following example we iterated over 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:

Following example will showcase all the above mentioned directives.

##### testAngularJS.htm

```
<html><title>AngularJS Directives</title><body>    <h1>Sample Application</h1>    <div ng-app="" ng-init="countries=[{locale:'en-IN',name:'India'},                                   
{locale:'en-GB',name:'United Kingdom'},                                   
{locale:'en-FR',name:'France'}]">        <p>Enter your Name: <input type="text" ng-model="username"></p>        <p>Welcome<span ng-bind="username"></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="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script></body></html>
```

##### Output:

Open textAngularJS.htm in a [web browser](https://answers.mindstick.com/qa/95505/what-are-the-pros-and-cons-of-using-safari-web-browser). Enter your name and see the result.

![AngularJs Directives](https://www.mindstick.com/mindstickarticle/f8f196a2-132a-4e26-87a9-f9e5e3dc7a88/images/31206a12-352a-404b-97c7-63b8c80bbc6f.png)

---

Original Source: https://www.mindstick.com/articles/1676/angularjs-directives

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
