In the previous articles, we have already discussed the different types of scope of the custom directives in
AngularJS. Now, in this article, we will discuss how to use inheritance in the custom directives. Actually, the inheritance word basically depends on OOPs. But as we know AngularJS is not a language which supports OOPs concepts. But directive inheritance means sometimes we need to create one or more custom directive which works only when it gets the specific value of any other directive. Suppose we want to create a shopping cart page when the shopping card directive will depend on the another directive called
qtyvalidate.
For this, we first create a html file named index.html and add the below code -
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Directive Types</title>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="../../Scripts/angular.min.js"></script>
- <script src="SampleDirective.js"></script>
- <script src="IndexController.js"></script>
-
- </head>
- <body data-ng-app="TestApp" data-ng-controller="IndexController as model">
- <div class="row animated fadeInRight">
- <div class="col-lg-12">
- <div class="rowDiv">
- <h3>{{model.message}}</h3>
- </div>
- </div>
- <div class="col-lg-12">
- <div class="rowDiv">
- <h2>Shopping Cart</h2>
- </div>
- </div>
- <div class="col-lg-12">
- <div class="rowDiv">
- <div class="col-lg-3" style="border:solid;border-width:thin; border-color:black;background-color :gray;font-weight:bold;"><span>Product</span></div>
- <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>
- <div class="col-lg-2" style="border:solid;border-width:thin;border-color:black;background-color :gray;font-weight:bold;"><span>Price</span></div>
- <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>
- <div class="col-lg-2"></div>
- </div>
- </div>
- <div class="col-lg-12">
- <shopping-cart item="IPhone" quantity="{{model.totalUnit}}" price="40000.00" unit="{{model.priceUnit}}" param="model.args" book="model.validateQuantity()"></shopping-cart>
- </div>
- </body>
- </html>
Now, add a javascript file named
indexcontroller.js and add the below code of the controller -
- testApp.controller('IndexController', ['$http', function ($http, urlService) {
- var self = this;
-
- self.message = 'Directive Inheritance';
- self.priceUnit = 'INR';
- self.totalUnit = 50;
- self.args = {
- quantity: 0
- };
-
- self.validateQuantity = function () {
- self.priceUnit = 'INR1';
- if (self.args.quantity == 0) {
- alert('Put valid quantity no for book');
- }
- else {
- if (self.args.quantity > self.totalUnit) {
- alert('Booked Qunatity above available Quantity');
- self.param.quantity = 0;
- return;
- }
- else {
- alert('Booked Quantity is Available');
- }
- }
- }
- }]);
Now, add another javascript file for defining the custom directive (file name -
SampleDirective.js) and create the first directive named
qtyvalidate which is basically an attribute type directive.
- 'use strict';
-
- var testApp = angular.module('TestApp', []);
-
- testApp.directive('qtyvalidate', function () {
- return {
- restrict: 'A',
- scope: true,
- controller: function () {
- this.validateStock = function (qty) {
- alert('Data Validate');
- }
- }
- }
- });
Now, add another directive named
shoppingCart which basically depends on the above mentioned directives.
- testApp.directive('shoppingCart', ['$http', function () {
- return {
- restrict: 'E',
- scope: {
- unit: '@'
- },
- bindToController: {
- param: '=',
- book: '&'
- },
- require: '^qtyvalidate',
- template: '<div class="rowDiv" qtyvalidate>' +
- ' <div class="col-lg-3">{{item}}</div>' +
- ' <div class="col-lg-2">{{qty}}</div>' +
- ' <div class="col-lg-2">{{price}} {{unit}}</div>' +
- ' <div class="col-lg-3"><input type="text" value="" data-ng-model="model.param.quantity" /></div>' +
- ' <div class="col-lg-1"><input type="button" value="Validate" class="btn-rounded" data-ng-click="model.book();" /></div>' +
- ' <div class="col-lg-1"><input type="button" value="Buy" class="btn-rounded" data-ng-click="model.validateData();" /></div>' +
- '</div>',
- templateUrl: 'SampleDirective.html',
- link: function (scope, element, attr, validateCtrl) {
- scope.item = attr.item;
- scope.qty = attr.quantity;
- scope.price = attr.price;
- scope.unit = '$';
- scope.validateCtrl = validateCtrl;
- },
- controllerAs: 'model',
- controller: function ($http) {
- var self = this;
-
- if (self.param == undefined) {
- self.param = {};
- }
-
- if (self.param.quantity == undefined) {
- self.param.quantity = 0;
- }
-
- self.validateData = function () {
- validateCtrl.validateStock(qty);
- }
-
- }
- }
- }]);
As you can see, the second directive, which is basically is a child directive, uses the keyword
require to reference the parent directives. All of you can see an earlier post related to tab control in angular js regarding this concept.
Read more articles on AngularJS: