Open In App

AngularJS Directives

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
102 Likes
Like
Report

Directives are markers in the Document Object Model(DOM). Directives can be used with any controller or HTML tag which will tell the compiler what exact operation or behavior is expected. There are some directives present that are predefined but if a developer wants he can create new directives (custom-directive).

List of Directives:

The following table lists the important built-in AngularJS directives with their brief description:

Directives

Description

ng-appStart of AngularJS application.
ng-initIt is used to initialize a variable
ng-modelIt is used to bind to the HTML controls
ng-controllerAttaches a controller to the view
ng-bindBinds the value with an HTML element
ng-repeatRepeats HTML template once per each item in the specified collection.
ng-showShows or hides the associated HTML element
ng-hideConditionally hides an HTML element based on the truthiness of an expression.
ng-readonlyMakes HTML element read-only
ng-disabledUse to disable or enable a button dynamically
ng-ifRemoves or recreates HTML element
ng-clickCustom step on click
ng-classConditionally applies CSS classes to an element based on the evaluation of expressions.
ng-submitBinds a function to the submit event of an HTML form, allowing execution of custom behavior on form submission.

We will be discussing a few of the Directives among the list of Directives given, with their basic implementation.

ng-app

The ng-app Directive in AngularJS is used to define the root element of an AngularJS application. This directive automatically initializes the AngularJS application on page load. It can be used to load various modules in AngularJS Applications. 

Example: This example uses ng-app Directive to define a default AngularJS application. 

Output:

ng-init

The ng-init directive is used to initialize an AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application. 

Example: In this example, we initialize an array of strings. 

Output:

ng-model

The ngModel is a directive that binds input, select, and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.

Example: This example describes the basic usage of the ngModel Directive in Angular JS.

Output:

ng-controller

The ng-controller Directive in AngularJS is used to add the controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions.

Example: This example describes the basic usage of the ng-controller Directive in Angular JS.

Output:

ng-bind

The ng-bind directive in AngularJS is used to bind/replace the text content of any particular HTML element with the value that is entered in the given expression. The value of specified HTML content updates whenever the value of the expression changes in the ng-bind directive. 

Example: This example describes the basic usage of the ng-bind Directive in Angular JS.

Output:

ng-repeat

Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects. 

The ng-repeat is similar to a loop that we have in C, C++, or other languages but technically it instantiates a template(normally a set of HTML structures) for each element in a collection that we are accessing. Angular maintains a $index variable as a key to the element which is currently being accessed and the user can also access this variable.

Example: This example illustrates the basic implementation of the ng-repeat directive in Angular JS.

  • Create an app.js file for the app. 
var app = angular.module('myApp',[]); 

app.controller('MainCtrl', function($scope){ 
    $scope.names = ['Adam','Steve','George','James','Armin']; 
    console.log($scope.names); 
});

Line 1- Created an app module named “myApp” with no dependencies. 

Line 3- Main controller for our application. 

Line 4- Array of strings “names”.

  • Create index.html page 
<!DOCTYPE html>
<html ng-app="myApp">

<head>
    <title>Angular ng-repeat</title>
    <script type="text/javascript" 
            src="jquery-3.2.1.min.js">
    </script>
    <script type="text/javascript" 
            src="angular.js">
      </script>
    <script type="text/javascript" 
            src="app.js">
      </script>
</head>

<body ng-controller="MainCtrl">
    <h2>Here is the name list</h2>
    <ul>
        <li ng-repeat="name in names">
            {{name}}
        </li>
    </ul>
</body>

</html>

Line 5- Include all the dependencies like jquery, angular-js, and app.js file 

Line 12- Use the ng-repeat directive to get one name from the names array at a time and display it. 

Output:

ng-show

The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements. 

Example: This example uses ng-show Directive to display the HTML element after checked the checkbox. 

Output:

ng-readonly

The ng-readonly Directive in AngularJS is used to specify the readonly attribute of an HTML element. The HTML element will be readonly only if the expression inside ng-readonly directive returns true. 

Example: This example uses ng-readonly Directive to enable readonly property. 

Output: 

ng-disabled

The ng-disabled Directive in AngularJS is used to enable or disable HTML elements. If the expression inside the ng-disabled attribute returns true then the form field will be disabled or vice versa. It is usually applied on the form field (input, select, button, etc).

Example: This example uses ng-disabled Directive to disable the button. 

Output:

ng-if: 

The ng-if Directive in AngularJS is used to remove or recreate a portion of an HTML element based on an expression. The ng-if is different from the ng-hide because it completely removes the element in the DOM rather than just hiding the display of the element. If the expression inside it is false then the element is removed and if it is true then the element is added to the DOM.

Example: This example changes the content after clicking the button. 

Output:

ng-if

ng-click:

The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can pop up an alert when the button is clicked.

Example: This example uses ng-click Directive to display an alert message after clicking the element. 

Output:

ng-click



Next Article

Similar Reads