Introduction
Scope in AngularJS inherits from Parent Scope default. But, sometimes it is not required specially in the case of development of some common components. In this case directive should not read or write properties value in the parent scope by mistake. So, Isolated Scope came into picture.
Scope in AngularJS inherits from Parent Scope default. But, sometimes it is not required specially in the case of development of some common components. In this case directive should not read or write properties value in the parent scope by mistake. So, Isolated Scope came into picture.
Isolated scope does not prototypically inherit from the parent scope. It can access its parent scope through the $parent property. So, Directive has three options for isolating its scope from parent scope. The following are the three options:
- scope: false - It is default in Directive. It lets to reuse the scope from the place where the component is being used.
- scope: true - It creates a child scope. This child scope prototypically inherits from the scope where the component is being used.
- scope: {...} - It creates Isolates scope. It does not prototypically inherit from the scope where the component is being used.
- scope: true
Parent Scope <<======prototype====== Child Scope
<<====== $Parent ====== Child Scope - scope: {}
Parent Scope <<=====$Parent Scope ========= Isolates Scope
There are three types of interface to specify between the element's attributes and the isolated scope:
- interpolate (@)
- data bind (=)
- expression (&)
These interfaces can be specified as key-value pairs on the scope property of the Directive on the element like the following:
- scope:
- {
- myValue1 : '@attribute1',
- myValue2: '=attribute2',
- myValue3: '&attribute3'
- }
So, we can interact with Isolated scope in three ways. Here's the explanation:
Attributes or Interpolate (@)
Attributes or Interpolate (@)
An Isolated scope property can be bind with DOM attributes. Interpolate or attribute sets up a one-way data binding from the Parent scope to the Isolated Scope of Directive. It means if Parent scope does any changes then changes will be reflected to the Isolated scope of Directive. But it will not reflect changes from Isolated scope to Parent scope.
Example:
- .directive('myDirective', function () {
- return {
- scope:{
- myAttribute:'@',
- }
- };
- })
- <my-directive my-attribute="{{Hello Geeks}}"></my-directive>
Binding works almost exactly like the attribute except that it provides two-way mode binding. It means changes can be seen from Parent to Isolated scope and vice-verse.
Example:
- .directive('myDirective', function () {
- return {
- scope:{
- myBinding:'=',
- }
- };
- })
Expression (&)
Expression is used to call a function on the Parent scope from the Isolated Scope. It is useful to create callbacks from the Directive component.
Expression is used to call a function on the Parent scope from the Isolated Scope. It is useful to create callbacks from the Directive component.
Example:
Directive
- .directive('myDirective', function () {
- return {
- scope:{
- myIsolatedFunction:'&'
- }
- };
- })
- <input ng-model="myIsolated">
- <button class="myButton" ng-click="myIsolatedFunction({myValue:myIsolated})">Click OK</button>
- .controller('myTestController', ['$scope', function ($scope) {
- $scope.myUpdatedValue= function (myValue) {
- $scope.updatedValue= myValue;
- }
- }]);
So, Isolated Scope concept is very important feature of AngularJS Directive. The usage of Isolated scope ensures that the scope inside and outside of Directive component will not contaminate each other. It is very useful and important because we don't want properties on the Parent scope affecting or being affected by what we do inside the control or template.
So, before developing any template or control by using Directive always try to focus on the concept of "Isolated Scope".
So, before developing any template or control by using Directive always try to focus on the concept of "Isolated Scope".
iqbal saudagarPosted Apr 10, 2017, 5:23 AM
Really it is very nice and easy to understand.
ali tauseefPosted Mar 14, 2016, 7:08 AM
Honestly went through the whole article and found it very useful for both novices and experts.
Santhakumar MunuswamyPosted Nov 20, 2015, 9:33 AM
Good One
Former memberPosted Nov 18, 2015, 1:17 PM
Thanks
Sibeesh VenuPosted Nov 18, 2015, 7:27 AM
Nice Share
Former memberPosted Nov 18, 2015, 1:32 AM
Thanks
Banketeshvar NarayanPosted Nov 18, 2015, 1:10 AM
Good One
Harshad PansuriyaPosted Nov 18, 2015, 1:03 AM
Nice one