Before reading this article, please go through the following article:
In my previous article, we discussed the different types of directives. Now, in this article, we will discuss the isolated scope of the directive. Now when we create element based directives, then we define two different types of scope within the directives. One of them is shared scope and another is isolated scope. Since Angular js always maintains its two-way binding technique when any element is updated within the directive, then it always updates all the related elements in the directive. Basically it is called shared scope. For a demonstration of this, we will create a page just like a shopping cart where multiple items will be displayed in tabular format and we can putin the required items and click on the buy buttons.
For this, we first create a js file named sampleDirective.js and write down the below code,
- 'use strict';
- var testApp = angular.module('TestApp', []);
- testApp.directive('shoppingCart', ['$http', function()
- {
- return
- {
- restrict: 'E',
- template: '<div class="rowDiv">' +
- ' <div class="col-lg-4">{{item}}</div>' +
- ' <div class="col-lg-2">{{quantity}}</div>' +
- ' <div class="col-lg-3"><input type="text" value="" data-ng-model="model.qty" /></div>' +
- ' <div class="col-lg-3"><input type="button" value="Buy" class="btn-rounded" data-ng-click="model.btnClick(item)" /></div>' +
- '</div>',
- link: function(scope, element, attr)
- {
- scope.item = attr.item;
- scope.quantity = attr.quantity;
- },
- controllerAs: 'model',
- controller: function($http)
- {
- var self = this;
- self.btnClick = function(item)
- {
- if (self.qty == undefined || self.qty == '')
- {
- alert('Put Quantity First')
- } else
- {
- alert('You have Booked ' + item + ' For ' + self.qty + ' No.');
- }
- }
- }
- }
- }]);



Sr KarthigaPosted Apr 19, 2016, 10:55 PM
Nice explanation
Sonu ChaudharyPosted Mar 5, 2016, 12:21 PM
Thanks
Pankaj Kumar ChoudharyPosted Mar 4, 2016, 11:46 AM
Nice Explain Sir........
Sibeesh VenuPosted Mar 4, 2016, 6:52 AM
Nice Share
Mohammed IbrahimPosted Mar 3, 2016, 10:38 AM
nice