{"id":32304,"date":"2016-02-11T20:21:24","date_gmt":"2016-02-11T14:51:24","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32304"},"modified":"2017-08-03T14:25:27","modified_gmt":"2017-08-03T08:55:27","slug":"scope-in-angularjs-custom-directive","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/scope-in-angularjs-custom-directive\/","title":{"rendered":"Scope in AngularJs Custom Directive"},"content":{"rendered":"<p>In AngularJs \u2018directive\u2019 plays an important role. <a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-getting-started-with-directives\/\">AngularJs custom directive<\/a> has many options like replace, scope, transclude etc. Here we will discuss \u2018scope\u2019 property.<\/p>\n<p>The scope can have 2 types of value:<br \/>\n1.Boolean<br \/>\n2.Object<\/p>\n<p><strong>1. Boolean<\/strong> :<br \/>\nWhen scope\u2019s value is false:-<br \/>\nIt defaults value. It creates a new scope but just similar to its parent scope means any changes in parent\u2019s scope reflects the directive\u2019s scope and vice versa.<\/p>\n<p>When scope&#8217;s value is true:-<br \/>\nIt creates a new scope. It is inherited scope and different from parent (controller) scope. If you make some changes in the directive scope then these changes will not reflect in the controller scope. But if you make some changes in parent scope, these changes will reflect in directive scope.<\/p>\n<p><strong>2. Object<\/strong>:<br \/>\nWhen scope\u2019s value is a blanobject: :-<br \/>\nIt creates an isolated scope. But we can access the parent scope.<\/p>\n<p>When scope\u2019s value is like<br \/>\n<code> {<br \/>\n'twoWayBindedTitle':'=newTitle',<br \/>\n'oneWayBindedTitle':'@readOnlyTitle',<br \/>\n'myInternalFunction':'&amp;myParentFunction'<br \/>\n}<\/code><\/p>\n<p><strong>Read only access<\/strong>: @ symbol tells that it has read only access. To make a read only variable we need to define as above. Here &#8216;oneWayBindedTitle&#8217; is the directive scope\u2019s variable and &#8216;readOnlyTitle&#8217; is the attribute of directive.<br \/>\n<strong>Two way binding<\/strong>: = symbol tells that it has read and write both access. To make a two way bind variable we need to define as above. Here &#8216;twoWayBindedTitle&#8217; is the directive scope\u2019s variable and &#8216;newTitle&#8217; is the attribute of the directive.<br \/>\n<strong>Method binding<\/strong>: Sometimes we need to access the parent scope\u2019s method in directive scope. So using &amp; symbol we can access them in directive scope. To access the parent scope\u2019s method in directive scope need to define it like above. Here &#8216;myInternalFunction&#8217; is the method in directive scope and &#8216;myParentFunction&#8217; is the attribute of the directive.<\/p>\n<p><strong>Controller Code: <\/strong><\/p>\n<p>[js]<br \/>\n$scope.title=&quot;Magic of Scope In Directive&quot;;<br \/>\nsetTimeout(function(){<br \/>\n   $scope.title=&#8217;Title is changed by Controller scope&#8217;;<br \/>\n   $scope.$digest();<br \/>\n},3000);<br \/>\n[\/js]<\/p>\n<p><strong>Directives Code:<\/strong><\/p>\n<p>[js]<br \/>\n  .directive(&#8216;directiveScopeValueIsFalse&#8217;, function () {<br \/>\n   return {<br \/>\n       scope : false, \/\/ we could leave this out, since it&#8217;s the default<br \/>\n       link : function ($scope, $element, $attrs) {<br \/>\n           setTimeout(function(){<br \/>\n               $scope.title = &#8216;Title is changed by Directive scope where scope is false&#8217;;<br \/>\n               $scope.$digest();<br \/>\n           },7000);<br \/>\n       }<br \/>\n   };<br \/>\n})<br \/>\n.directive(&#8216;directiveScopeValueIsTrue&#8217;, function () {<br \/>\n   return {<br \/>\n       scope : true,<br \/>\n       link : function ($scope, $element, $attrs) {<br \/>\n        setTimeout(function(){<br \/>\n               $scope.title = &#8216;Title is changed by Directive scope where scope is true&#8217;;<br \/>\n               $scope.$digest();<br \/>\n           },10000);<br \/>\n       }<br \/>\n   };<br \/>\n})<br \/>\n.directive(&#8216;myComponent&#8217;, function () {<br \/>\n   return {<br \/>\n       restrict:&#8217;E&#8217;,<br \/>\n       scope : {<br \/>\n           &#8216;twoWayBindedTitle&#8217;:&#8217;=newTitle&#8217;,<br \/>\n           &#8216;oneWayBindedTitle&#8217;:&#8217;@readOnlyTitle&#8217;<br \/>\n       },<br \/>\n       link : function ($scope, $element, $attrs) {<br \/>\n               setTimeout(function(){<br \/>\n               $scope.twoWayBindedTitle = &#8216;Title is changed by Directive scope where scope is object&#8217;;<br \/>\n               $scope.$digest();<br \/>\n           },13000);<br \/>\n       },<br \/>\n       templateUrl:&#8217;component&#8217;<br \/>\n   };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p><strong>Html Code:<\/strong><\/p>\n<p>[js]<\/p>\n<p>   Controller(parent) scope<br \/>\n   &lt;h2&gt;{{title}}&lt;\/h2&gt;<br \/>\n   &lt;div&gt;<br \/>\n       &lt;h3&gt;Directive Scope &#8212; scope= false&lt;\/h3&gt;<br \/>\n       &lt;p&gt;Title&#8212;&#8212;-&gt;{{title}}&lt;\/p&gt;<br \/>\n   &lt;\/div&gt;<br \/>\n   &lt;div&gt;<br \/>\n       &lt;h3&gt;Directive Scope &#8212; scope= true&lt;\/h3&gt;<br \/>\n       &lt;p&gt;Title&#8212;&#8212;&gt; {{title}}&lt;\/p&gt;<br \/>\n   &lt;\/div&gt;<\/p>\n<p>   &lt;h3&gt;Directive Scope &#8212;-&gt; scope= Object&lt;\/h3&gt;<br \/>\n   &lt;p&gt;Read Only varible(title):&#8212;&#8212;&gt;  {{oneWayBindedTitle}}&lt;\/p&gt;<br \/>\n   &lt;p&gt;Two way Binded variable(title): &#8212;&#8212;&gt; {{twoWayBindedTitle}}&lt;\/p&gt;<\/p>\n<p>[\/js]<\/p>\n<p>When you will try to run the above mentioned code, the in output values of the variables will be<\/p>\n<p><code>title=\u201cMagic of Scope In Directive\u201d<br \/>\noneWayBindedTitle=\u201cMagic of Scope In Directive\u201d<br \/>\ntwoWayBindedTitle=\u201cMagic of Scope In Directive\u201d<br \/>\n<\/code><br \/>\n<strong>After 3 seconds:<\/strong><\/p>\n<p><code>title=\u201cTitle is changed by Controller scope\u201d<br \/>\noneWayBindedTitle=\u201cTitle is changed by Controller scope\u201d<br \/>\ntwoWayBindedTitle=\u201cTitle is changed by Controller scope\u201d<\/code><\/p>\n<p><strong>After 7 seconds:<\/strong><\/p>\n<p><code>title=\u201cTitle is changed by Directive scope where scope is false\u201d<br \/>\noneWayBindedTitle=\u201cTitle is changed by Directive scope where scope is false\u201d<br \/>\ntwoWayBindedTitle=\u201cTitle is changed by Directive scope where scope is false\u201d<\/code><\/p>\n<p><strong>After 10 seconds:<\/strong><\/p>\n<p>In Controller and &#8216;directiveScopeValueIsFalse&#8217; directive Scope:<br \/>\n<code>title=\u201cTitle is changed by Directive scope where scope is false\u201d<\/code><\/p>\n<p>In &#8216;directiveScopeValueIsTrue&#8217; directive scope:<br \/>\n<code>title=\"Title is changed by Directive scope where scope is true\"<br \/>\noneWayBindedTitle=\u201cTitle is changed by Directive scope where scope is false\u201d<br \/>\ntwoWayBindedTitle=\u201cTitle is changed by Directive scope where scope is false\u201d<\/code><\/p>\n<p><strong>After 13 seconds:<\/strong><br \/>\nIn Controller and &#8216;directiveScopeValueIsFalse&#8217; directive Scope:<br \/>\n<code>title=\u201cTitle is changed by Directive scope where scope is false\u201d<\/code><\/p>\n<p>In &#8216;directiveScopeValueIsTrue&#8217; directive scope:<br \/>\n<code>title=\"Title is changed by Directive scope where scope is true\"<br \/>\noneWayBindedTitle=\u201cTitle is changed by Directive scope where scope is false\u201d<br \/>\ntwoWayBindedTitle=\u201cTitle is changed by Directive scope where scope is object\u201d<\/code><\/p>\n<p>Hope this will help you to understand the &#8216;scope&#8217; in Custom Directive.<\/p>\n<p>Read Further on AngularJS Series<\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In AngularJs \u2018directive\u2019 plays an important role. AngularJs custom directive has many options like replace, scope, transclude etc. Here we will discuss \u2018scope\u2019 property. The scope can have 2 types of value: 1.Boolean 2.Object 1. Boolean : When scope\u2019s value is false:- It defaults value. It creates a new scope but just similar to its [&hellip;]<\/p>\n","protected":false},"author":148,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[1439,3429,1],"tags":[3955,4697],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32304"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/148"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32304"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32304\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}