AngularJS Directives

Directives – Directives are attributes decorated on HTML elements. All directives start with the word “ng”. AngularJS has a set of built in directives which offers functionality to the application. You can also define your own directives in AngularJS

Let me explain few directives here: 
  • ng-app – This directive initializes and AngularJS application.
  • ng-bind – This directive binds the content of an HTML element to application data.
  • ng-class - This directive specifies CSS classes on HTML elements.
  • ng-checked – This directive specifies if an element is checked or not.
  • ng-click – This directive specifies and expression to evaluate when and element is being clicked.
  • ng-controller – This directive defines the controller object for an application.
  • ng-init – This directive initializes application data.
  • ng-model – This directive binds the value of HTML controls to application data.
  • ng-selected – This directive specifies the selected attribute of an element.
  • ng-repeat – This directive defines a template for each data in a collection.
  • ng-required – This directive specifies the required attribute of an element.
  • ng-show – This directive defines show and hide HTML elements.
  • ng-submit – This directive specifies the expressions to run on onsubmit event
  • ng-value – This directive specifies the value of an input element. 

 ng-app

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. <script src="Scripts/angular-route.min.js"></script>  
  14.   
  15. <script src="app.js"></script>  
  16.   
  17. <script src="controller.js"></script>  
  18.   
  19. </head>  
  20.   
  21. <body>  
  22.   
  23. <div ng-app="app" ng-controller="controller">  
  24.   
  25. <h1> {{ title }} {{ date }}</h1>  
  26.   
  27. </div>  
  28.   
  29. </body>  
  30.   
  31. </html>  
In given example, you can see I have added reference of a module name of app.js and assigned app name to div in ng-app directive, you can leave this blank if you don’t want to use any module. 

Other way is you can write app and controller code inside script tag on same page like this.

  1. <div ng-app="app" ng-controller="controller">  
  2.   
  3. <h1> {{ title }} {{ date }}</h1>  
  4.   
  5. </div>  
  6.   
  7.   
  8. <script>  
  9.   
  10. var app = angular.module("app", []);  
  11.   
  12. app.controller("controller", function ($scope) {  
  13.   
  14. $scope.title = "This is test message by RB! on ";  
  15.   
  16. $scope.date = new Date();  
  17.   
  18. });  
  19.   
  20. </script>  

  

ng-bind

  1. <div ng-app="app" ng-init="title='This is test message by RB!'">  
  2.   
  3. <p ng-bind="title"></p>  
  4.   
  5. </div>  

OR 

  1. <div ng-app="app" ng-controller="controller">  
  2.   
  3. <label>Title: <input type="text" ng-model="title"></label><br>  
  4.   
  5. <h1 ng-bind="title"></h1><h3 ng-bind="date"></h3>  
  6.   
  7. </div>  
  8.   
  9. <script>  
  10.   
  11. var app = angular.module("app", []);  
  12.   
  13. app.controller("controller", function ($scope) {  
  14.   
  15. $scope.title = "This is test message by RB! on ";  
  16.   
  17. $scope.date = new Date();  
  18.   
  19. });  
  20.   
  21. </script>  

ng-class

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. <style>  
  14.   
  15. .CSharp {  
  16.   
  17. color: white;  
  18.   
  19. background-color: lightblue;  
  20.   
  21. padding: 20px;  
  22.   
  23. font-family: "Courier New";  
  24.   
  25. }  
  26.   
  27. .ASPNET {  
  28.   
  29. background-color: coral;  
  30.   
  31. padding: 40px;  
  32.   
  33. font-family: Verdana;  
  34.   
  35. }  
  36.   
  37. .VBNET {  
  38.   
  39. background-color: brown;  
  40.   
  41. padding: 40px;  
  42.   
  43. font-family: Verdana;  
  44.   
  45. }  
  46.   
  47. .red {  
  48.   
  49. color: red;  
  50.   
  51. }  
  52.   
  53. .green {  
  54.   
  55. color: green;  
  56.   
  57. }  
  58.   
  59. .blue {  
  60.   
  61. color: blue;  
  62.   
  63. }  
  64.   
  65. </style>  
  66.   
  67. </head>  
  68.   
  69. <body ng-app="">  
  70.   
  71. <p>Choose a technology:</p>  
  72.   
  73. <select ng-model="home">  
  74.   
  75. <option value="CSharp">C#</option>  
  76.   
  77. <option value="ASPNET">ASP.NET</option>  
  78.   
  79. <option value="VBNET">VB.NET</option>  
  80.   
  81. </select>  
  82.   
  83. <div ng-class="home">  
  84.   
  85. <h1>This is test message by RB!</h1>  
  86.   
  87. </div>  
  88.   
  89. <div>  
  90.   
  91. <button ng-click="areaStatus = !areaStatus">Toggle</button>  
  92.   
  93. <div ng-class="{'red' : areaStatus, 'green' : !areaStatus}">  
  94.   
  95. This is some text  
  96.   
  97. </div>  
  98.   
  99. </div>  
  100.   
  101. </body>  
  102.   
  103. </html>  

ng-checked

  1. <div ng-app="app" ng-controller="controller">  
  2.   
  3. Check Me: <input type="checkbox" ng-model="checked">  
  4.   
  5. This Will Check Automatically: <input id="checkTest" type="checkbox" ng-checked="checked">  
  6.   
  7. </div>  
  8.   
  9. <br />  
  10.   
  11. <script>  
  12.   
  13. var app = angular.module("app", []);  
  14.   
  15. app.controller("controller", function ($scope) {  
  16.   
  17. $scope.checked = false;  
  18.   
  19. });  
  20.   
  21. </script>  

 

Or

  1. <div ng-app="">  
  2.   
  3. <p>Fav Technologies:</p>  
  4.   
  5. <input type="checkbox" ng-model="all"> Check all<br><br>  
  6.   
  7. <input type="checkbox" ng-checked="all">C#<br>  
  8.   
  9. <input type="checkbox" ng-checked="all">ASP.NET<br>  
  10.   
  11. <input type="checkbox" ng-checked="all">VB.NET<br />  
  12.   
  13. <input type="checkbox" ng-checked="all">MVC<br />  
  14.   
  15. <input type="checkbox" ng-checked="all">AngularJS  
  16.   
  17. </div>  

In above screen when you click on check all check box all check boxes will be checked.

ng-click

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" ng-controller="controller">  
  18.   
  19. <button ng-click="myFunc()">OK</button>  
  20.   
  21. <p>The button has been clicked {{count}} times.</p>  
  22.   
  23. <button ng-click="submit()">Submit</button>  
  24.   
  25. <p>Alert - {{msg}}</p>  
  26.   
  27. </div>  
  28.   
  29. <script>  
  30.   
  31. var app = angular.module("app", []);  
  32.   
  33. app.controller("controller", function ($scope) {  
  34.   
  35. $scope.count = 0;  
  36.   
  37. $scope.myFunc = function () {  
  38.   
  39. $scope.count++;  
  40.   
  41. };  
  42.   
  43. $scope.submit = function () {  
  44.   
  45. $scope.msg = "Congrats! you clicked a submit button.";  
  46.   
  47. };  
  48.   
  49. });  
  50.   
  51. </script>  
  52.   
  53. </body>  
  54.   
  55. </html>  

In given code, we have two buttons and two functions. One button invokes a function which increase a number and other function display a message when click on submit button.

ng-controller

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" ng-controller="controller">  
  18.   
  19. <h2>Hello! My Name is {{firstname}} {{lastname}}</h2>  
  20.   
  21. </div>  
  22.   
  23. <script>  
  24.   
  25. var app = angular.module("app", []);  
  26.   
  27. app.controller("controller", function ($scope) {  
  28.   
  29. $scope.firstname = "Raj";  
  30.   
  31. $scope.lastname = "Kumar";  
  32.   
  33. });  
  34.   
  35. </script>  
  36.   
  37. </body>  
  38.   
  39. </html>  

ng-init

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" ng-init="title='This is C# Contribution of Raj Kumar!';TotalArticles=288;TotalBlogs=49">  
  18.   
  19. Article: <input type="text" ng-model="TotalArticles" />  
  20.   
  21. Blogs: <input type="text" ng-model="TotalBlogs" />  
  22.   
  23. Total Contribution: {{TotalArticles + TotalBlogs}}  
  24.   
  25. <div>  
  26.   
  27. <p style="font-size:24px;" ng-bind="title"></p>  
  28.   
  29. </div>  
  30.   
  31. </div>  
  32.   
  33.   
  34. <script>  
  35.   
  36. var app = angular.module("app", []);  
  37.   
  38. </script>  
  39.   
  40. </body>  
  41.   
  42. </html>  

ng-model

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" ng-controller="controller">  
  18.   
  19. Name: <input ng-model="name" /><br /><br />  
  20.   
  21. Website: <input ng-model="website" /><br /><br />  
  22.   
  23. Articles: <input ng-model="articles" /><br /><br />  
  24.   
  25. Blogs: <input ng-model="blogs"/>  
  26.   
  27. </div>  
  28.   
  29. <script>  
  30.   
  31. var app = angular.module("app", []);  
  32.   
  33. app.controller("controller", function ($scope) {  
  34.   
  35. $scope.name = "Raj Kumar";  
  36.   
  37. $scope.website = "C# Corner";  
  38.   
  39. $scope.articles = "288";  
  40.   
  41. $scope.blogs = "49";  
  42.   
  43. });  
  44.   
  45. </script>  
  46.   
  47. </body>  
  48.   
  49. </html>  

ng-selected

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" style="font-size:24px;">  
  18.   
  19. Click here to select ASP.NET as language:  
  20.   
  21. <input type="checkbox" ng-model="selected">  
  22.   
  23. <p>Languages:</p>  
  24.   
  25. <select>  
  26.   
  27. <option>C#</option>  
  28.   
  29. <option ng-selected="selected">ASP.NET</option>  
  30.   
  31. <option>VB.NET</option>  
  32.   
  33. <option>MVC</option>  
  34.   
  35. <option>AngularJS</option>  
  36.   
  37. </select>  
  38.   
  39. </div>  
  40.   
  41. <script>  
  42.   
  43. var app = angular.module("app", []);  
  44.   
  45. </script>  
  46.   
  47. </body>  
  48.   
  49. </html>  

 

ng-repeat

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <table ng-app="app" ng-controller="controller" cellpadding="5" cellspacing="5" border="1">  
  18.   
  19. <thead>  
  20.   
  21. <tr>  
  22.   
  23. <th>Title</th>  
  24.   
  25. <th>Date</th>  
  26.   
  27. <th>Views</th>  
  28.   
  29. </tr>  
  30.   
  31. </thead>  
  32.   
  33. <tr ng-repeat="obj in articles">  
  34.   
  35. <td>{{obj.title}}</td>  
  36.   
  37. <td>{{obj.date}}</td>  
  38.   
  39. <td>{{obj.view}}</td>  
  40.   
  41. </tr>  
  42.   
  43. </table>  
  44.   
  45. <script>  
  46.   
  47. var app = angular.module("app", []);  
  48.   
  49. app.controller("controller", function ($scope) {  
  50.   
  51. $scope.articles = [  
  52.   
  53. {  
  54.   
  55. "title": "AngularJS Filters",  
  56.   
  57. "date": "Aug 29, 2016",  
  58.   
  59. "view": "9947"  
  60.   
  61. },  
  62.   
  63. {  
  64.   
  65. "title": "AngularJS CRUD Operations with Web API Using SQL Database",  
  66.   
  67. "date": "Aug 16, 2016",  
  68.   
  69. "view": "42522"  
  70.   
  71. },  
  72.   
  73. {  
  74.   
  75. "title": "AngularJS Routing",  
  76.   
  77. "date": "Aug 15, 2016",  
  78.   
  79. "view": "4221"  
  80.   
  81. },  
  82.   
  83. {  
  84.   
  85. "title": "How to make Pie Chart using D3 with AngularJS",  
  86.   
  87. "date": "Aug 05, 2016",  
  88.   
  89. "view": "5835"  
  90.   
  91. },  
  92.   
  93. {  
  94.   
  95. "title": "Register Bot with Microsoft Bot Framework",  
  96.   
  97. "date": "Jul 01, 2016",  
  98.   
  99. "view": "6938"  
  100.   
  101. }  
  102.   
  103. ]  
  104.   
  105. });  
  106.   
  107. </script>  
  108.   
  109. </body>  
  110.   
  111. </html>  

ng-required

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" ng-controller="controller">  
  18.   
  19. <form name="articleForm">  
  20.   
  21. <label for="inputTitle">Title:</label>  
  22.   
  23. <input type="text" id="inputTitle" name="inputTitle" ng-model="title" ng-required="isRequired" />  
  24.   
  25. <p style="color:red;" ng-if="!articleForm.inputTitle.$valid">Title is required!</p>  
  26.   
  27. <br /><br />  
  28.   
  29. <label for="inputKeyword">Keyword:</label>  
  30.   
  31. <input type="text" id="inputKeyword" name="inputKeyword" ng-model="keyword" ng-required="isRequired" />  
  32.   
  33. <p style="color:red;" ng-if="!articleForm.inputKeyword.$valid">Keyword is required!</p>  
  34.   
  35. <br /><br />  
  36.   
  37. <label for="inputSummary">Summary:</label>  
  38.   
  39. <textarea type="text" id="inputSummary" name="inputSummary" ng-model="Summary" ng-minlength="100" ng-maxlength="1000" ng-required="isRequired"></textarea>  
  40.   
  41. <p style="color:red;" ng-if="!articleForm.inputSummary.$valid">Summary is required!</p>  
  42.   
  43. <div>  
  44.   
  45. <button type="submit" class="button" ng-click="checkValidate()">Submit!</button>  
  46.   
  47. </div>  
  48.   
  49. </form>  
  50.   
  51. </div>  
  52.   
  53. <script>  
  54.   
  55. var app = angular.module("app", []);  
  56.   
  57. app.controller("controller", function ($scope) {  
  58.   
  59. $scope.isRequired = false;  
  60.   
  61. $scope.checkValidate = function () {  
  62.   
  63. $scope.isRequired = true;  
  64.   
  65. }  
  66.   
  67. });  
  68.   
  69. </script>  
  70.   
  71. </body>  
  72.   
  73. </html>  

ng-show

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app">  
  18.   
  19. Show my Bio: <input type="checkbox" ng-model="myVar">  
  20.   
  21. <div ng-show="myVar">  
  22.   
  23. <h1>About Me</h1>  
  24.   
  25. <p>Raj Kumar is two time Microsoft MVP and four time C# Corner MVP.</p>  
  26.   
  27. </div>  
  28.   
  29. </div>  
  30.   
  31. <script>  
  32.   
  33. var app = angular.module("app", []);  
  34.   
  35. </script>  
  36.   
  37. </body>  
  38.   
  39. </html>  

ng-submit

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app">  
  18.   
  19. <form name="articleForm" ng-controller="controller" ng-submit="submitData(title, keyword, summary)">  
  20.   
  21. <label>Title:</label>  
  22.   
  23. <input type="text" name="form.title" ng-model="title" />  
  24.   
  25. <br /><br />  
  26.   
  27. <label>Keyword:</label>  
  28.   
  29. <input type="text" name="form.keyword" ng-model="keyword" />  
  30.   
  31. <br /><br />  
  32.   
  33. <label>Summary:</label>  
  34.   
  35. <textarea type="text" name="form.summary" ng-model="summary" ng-minlength="100" ng-maxlength="1000"></textarea>  
  36.   
  37. <br /><br />  
  38.   
  39. <div>  
  40.   
  41. <button type="submit">Submit!</button>  
  42.   
  43. </div>  
  44.   
  45. </form>  
  46.   
  47. </div>  
  48.   
  49. <script>  
  50.   
  51. var app = angular.module("app", []);  
  52.   
  53. app.controller("controller", function ($scope) {  
  54.   
  55. $scope.submitData = function (title, keyword, summary) {  
  56.   
  57. alert(title);  
  58.   
  59. alert("Data saved successfully!");  
  60.   
  61. }  
  62.   
  63. });  
  64.   
  65. </script>  
  66.   
  67. </body>  
  68.   
  69. </html>  

ng-value

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title></title>  
  8.   
  9. <meta charset="utf-8" />  
  10.   
  11. <script src="Scripts/angular.min.js"></script>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17. <div ng-app="app" ng-controller="controller">  
  18.   
  19. <input ng-value="name" />  
  20.   
  21. <input ng-value="location" />  
  22.   
  23. <input ng-value="type" />  
  24.   
  25. </div>  
  26.   
  27. <script>  
  28.   
  29. var app = angular.module("app", []);  
  30.   
  31. app.controller("controller", function ($scope) {  
  32.   
  33. $scope.name = "Raj Kumar";  
  34.   
  35. $scope.location = "USA";  
  36.   
  37. $scope.type = "Author";  
  38.   
  39. });  
  40.   
  41. </script>  
  42.   
  43. </body>  
  44.   
  45. </html>  

Conclusion
In this article, we have learnt the basic examples of AngularJS directives. If you have any question or comments, then drop me a line in C# Corner comments section or download attached sample code.


Similar Articles