Custom Directive In AngularJS - Part Three

Before reading this article, I highly recommend reading the previous part of the series on AngularJS:

In the previous article, we discussed the different types of directives including isolated scope of the directives. Now, in this article, we will discuss the directive scope of the directive. The main advantage of directive scope is that it is areusable component that can easily be used multiple times. Also, we can provide multiple additional values to the directives and also bind those values in directives. For this purpose, we use:

  1. “@” scope: This type of scope is used for passing value to the directive scope.

  2. “=” scope: This type of scope is used for passing the variables instead of the scope values. The purpose of doing this is to implement two way binding in the directive.

  3. “&” scope: This type of scope is used for pass the value and its reference to the directives. In this scope, we can pass the expression to the directives.
For doing this, we first create a js file with name SampleDirective.js and write the following code for the directive:
  1. 'use strict';  
  2.   
  3. var testApp = angular.module('TestApp', []);  
  4. testApp.directive('shoppingCart', ['$http'function () {  
  5.     return {  
  6.         restrict: 'E',  
  7.         scope: {  
  8.             unit: '@'  
  9.         },  
  10.         bindToController: {  
  11.             param: '=',  
  12.             book: '&'  
  13.         },  
  14.         template: '<div class="rowDiv">' +  
  15.                     '    <div class="col-lg-3">{{item}}</div>' +  
  16.                     '    <div class="col-lg-2">{{qty}}</div>' +  
  17.                     '    <div class="col-lg-2">{{price}} {{unit}}</div>' +  
  18.                     '    <div class="col-lg-3"><input type="text" value="" data-ng-model="model.param.quantity" /></div>' +  
  19.                     '    <div class="col-lg-2"><input type="button" value="Buy" class="btn-rounded" data-ng-click="model.book();" /></div>' +  
  20.                     '</div>',  
  21.         link: function (scope, element, attr) {  
  22.             scope.item = attr.item;  
  23.             scope.qty = attr.quantity;  
  24.             scope.price = attr.price;  
  25.             scope.unit = '$';  
  26.         },  
  27.         controllerAs: 'model',  
  28.         controller: function ($http) {  
  29.             var self = this;  
  30.   
  31.             if (self.param == undefined) {  
  32.                 self.param = {};  
  33.             }  
  34.   
  35.             if (self.param.quantity == undefined) {  
  36.                 self.param.quantity = 0;  
  37.             }  
  38.   
  39.         }  
  40.     }  
  41. }]);  
Now, add an html file name Index.html  and write the following code:
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.   
  4. <head>  
  5.     <title>Directive Types</title>  
  6.     <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />  
  7.     <script src="../../Scripts/angular.min.js"></script>  
  8.     <script src="SampleDirective.js"></script>  
  9.     <script src="IndexController.js"></script>  
  10. </head>  
  11.   
  12. <body data-ng-app="TestApp" data-ng-controller="IndexController as model">  
  13.     <div class="row animated fadeInRight">  
  14.         <div class="col-lg-12">  
  15.             <div class="rowDiv">  
  16.                 <h3>{{model.message}}</h3> </div>  
  17.         </div>  
  18.         <div class="col-lg-12">  
  19.             <div class="rowDiv">  
  20.                 <h2>Shopping Cart</h2> </div>  
  21.         </div>  
  22.         <div class="col-lg-12">  
  23.             <div class="rowDiv">  
  24.                 <div class="col-lg-3" style="border:solid;border-width:thin; border-color:black;background-color :gray;font-weight:bold;"><span>Product</span></div>  
  25.                 <div class="col-lg-2" style="border:solid;border-width:thin;border-color:black;background-color :gray;font-weight:bold;"><span>Available Quantity</span></div>  
  26.                 <div class="col-lg-2" style="border:solid;border-width:thin;border-color:black;background-color :gray;font-weight:bold;"><span>Price</span></div>  
  27.                 <div class="col-lg-3" style="border:solid;border-width:thin;border-color:black;background-color :gray;font-weight:bold;"><span>Taken Qty</span></div>  
  28.                 <div class="col-lg-2"></div>  
  29.             </div>  
  30.         </div>  
  31.         <div class="col-lg-12">  
  32.             <shopping-cart item="IPhone" quantity="{{model.totalUnit}}" price="40000.00" unit="{{model.priceUnit}}" param="model.args" book="model.validateQuantity()"></shopping-cart>  
  33.         </div>  
  34. </body>  
  35.   
  36. </html>  
Now add another js file named indexcontroller.js and write the following code - 
  1. testApp.controller('IndexController', ['$http'function ($http, urlService) {  
  2.     var self = this;  
  3.   
  4.     self.message = 'Directive Scope';  
  5.     self.priceUnit = 'INR';  
  6.     self.totalUnit = 50;  
  7.     self.args = {  
  8.         quantity: 0  
  9.     };  
  10.   
  11.     self.validateQuantity = function () {  
  12.         self.priceUnit = 'INR1';  
  13.         if (self.args.quantity == 0) {  
  14.             alert('Put valid quantity no for book');  
  15.         }  
  16.         else {  
  17.             if (self.args.quantity > self.totalUnit) {  
  18.                 alert('Booked Qunatity above available Quantity');  
  19.                 self.param.quantity = 0;  
  20.                 return;  
  21.             }  
  22.             else {  
  23.                 alert('Booked Quantity is Available');  
  24.             }  
  25.         }  
  26.     }  
  27. }]);  
Now, run the code and the output will be like below, where we pass the validateQuantity() method to the directive to validate the given quantity. Also, we pass parameters to the directive for other scope variables.

run

output

Read more articles on AngularJS: