Loader Directive In AngularJS

Introduction

Directive is a very powerful feature of AngularJS. Directives are HTML remarks for an HTML DOM element, like  an attribute (Restrict -A), Element name (Restrict -E), Comment(Restrict -C). It tells the HTML compiler of  AngularJS to perform a specific behavior to that DOM element or even transform the DOM element and its children. 
 
I will create one loader component by using directive.
 
Shown below is the code of AngularJS loader, by using directive.
  1. var app = angular.module("myapp");  
  2.   
  3. app.directive('loading'function () {  
  4.     return {  
  5.         restrict: 'E',  
  6.         replace: true,  
  7.         scope: {  
  8.             showme: '=',  
  9.             showerror: '=',  
  10.             errormessage: '='  
  11.         },  
  12.   
  13.         templateUrl: 'components/loading.html',  
  14.     }  
  15. })  
loading.html code 
  1. <div class="container">  
  2.     <div ng-if="showme" class="showme-loader">  
  3.         <img ng-src="ajax-loader.gif" width="20" height="20" />  
  4.         Please wait...  
  5.     </div>  
  6.     <div ng-if="showerror">  
  7.         <h4>{{errormessage}}</h4>  
  8.     </div>  
  9. </div>  
Consider the following points related to this loading directive -
  1. Directive name is loading.
  2. It is restricted to Elements only.
  3. Three values can be passed by using three different attributes like showme, showerror, errormessage.
Now, use this directive as shown below.
 
Controller -
  1. var app = angular.module("myapp");  
  2.   
  3. app.controller('homeController', ['$scope''$http''$q'function ($scope, $http, $q) {  
  4.     var controller = this;  
  5.     controller.title = "Loader Directive Example.";  
  6.     controller.isLoading = false;  
  7.     controller.errorMessage = "";  
  8.   
  9.     controller.getData = function () {  
  10.         controller.isLoading = true;  
  11.         var deferred = $q.defer();  
  12.         $http.get('https://abcd').success(function (data) {  
  13.               deferred.resolve(data);  
  14.               controller.isLoading = false;  
  15.           }).error(function (err) {  
  16.               controller.isLoading = false;  
  17.               controller.errorMessage = "OOPS! Error While Fetching Data from Server.";  
  18.               console.log('Error retrieving data');  
  19.               deferred.reject(err);  
  20.           });  
  21.         return deferred.promise;  
  22.     }  
  23.   
  24.     controller.getData();  
  25. }]);  
Here, Controller's method "getData" is trying to make "$http.get" service call. Value for "controller.isLoading" has been assigned to true before making service call. It will be assigned to false in case of success or failure from service.
It will set "errorMessage" in case of failure of service.
 
Now, I will use this code in HTML file.
  1. <div>  
  2.     <h1>{{homeController.title}}</h1>  
  3. </div>  
  4. <div>  
  5.     <loading showme="homeController.isLoading" showerror="true" errormessage="homeController.errorMessage"></loading>  
  6. </div>  
Here, loading directive has been used as an element. And, three different values have been passed into the attribute of loading directive.
 
Find the attached zip file of project for the complete source code.

Conclusion

AngularJS directives are very powerful and important features. Developers can create new html components in the form of element, attribute, comments, etc.

Ebook Download
View all
Learn
View all