{"id":14461,"date":"2014-06-30T23:08:42","date_gmt":"2014-06-30T17:38:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14461"},"modified":"2017-03-06T10:50:24","modified_gmt":"2017-03-06T05:20:24","slug":"notation-symbols-in-isolated-scope","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/notation-symbols-in-isolated-scope\/","title":{"rendered":"Notation Symbols in Isolated Scope"},"content":{"rendered":"<p>In my <strong>previous blog<\/strong> we studied the <strong>scope<\/strong> in <strong>AngularJS<\/strong>(we read three types of <strong>scope<\/strong> in a <strong>directive<\/strong>, <strong>Parent Scope<\/strong>, <strong>Inherited Scope<\/strong> and <strong>Isolated Scope<\/strong>). <strong>Isolated Scope<\/strong> was completely <strong>Isolated <\/strong>after <strong>AngularJS<\/strong>-1.2 so we can not access <strong>Parent Scope directly<\/strong>. But sometimes we face a requirement where we want to access <strong>Parent Scope<\/strong> property\/methods into <strong>Isolated Scope<\/strong>, then <strong>Notation Symbols<\/strong> get into the role, and they help us to access <strong>parent scope<\/strong> property\/methods. There are three types of <strong>Notations Symbols<\/strong>:<\/p>\n<p>1. <strong>@ (Read Only\/1-way-binding) Notation Symbol<\/strong><br \/>\n2. <strong>= (2-way-binding) Notation Symbol<\/strong><br \/>\n3. <strong>&amp; (Function) Notation Symbol<\/strong><\/p>\n<p>1. <strong>@ (Read Only\/1-way-binding) Notation Symbol<\/strong>: If we bind a property from <strong>Parent Scope<\/strong> to <strong>Child Directive Scope<\/strong> via <strong>@ notation<\/strong>, then, whatever change is made to the <strong>Parent Scope<\/strong> property the same will automatically reflect to <strong>Child Directive Scope<\/strong> property also. But if we make change to <strong>Child Directive Scope<\/strong> property then that change will not be reflected in <strong>Parent Scope<\/strong> property. So we call it a <strong>Read Only Notation<\/strong> or <strong>1-way-binding Notation<\/strong>.<\/p>\n<p>Defining a <strong>Isolated Scope Directive<\/strong> named as <strong>readOnlyNotationSymbol<\/strong> below:<br \/>\n[js]<br \/>\nnotationSymbolsTestApp.directive(&#8216;readOnlyNotationSymbol&#8217;, function () {<br \/>\n    return {<br \/>\n        scope: {<br \/>\n            readOnlyAge: &#8216;@userAge&#8217;<br \/>\n        },<br \/>\n        link: function (scope) {<br \/>\n            scope.increaseAge = function () {<br \/>\n                scope.readOnlyAge++;<br \/>\n            };<br \/>\n        },<br \/>\n        template: &amp;quot;&amp;lt;div&amp;gt;Read Only Age: {{readOnlyAge}}&amp;lt;\/div&amp;gt; &amp;lt;input type=&#8217;button&#8217; value=&#8217;Update Read Only Age&#8217; ng-click=&#8217;increaseAge()&#8217;&amp;gt;&amp;quot;<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>Now we have to use above <strong>readOnlyNotationSymbol<\/strong> <strong>directive<\/strong> as below, and we have to provide age value to user-age attribute which will be assigned to <strong>Isolate Directive Scope<\/strong> property named readOnlyAge with the help of <strong>@ Notation Symbol<\/strong>.<br \/>\n[html]<br \/>\n&amp;lt;div read-only-notation-symbol user-age=&amp;quot;{{age}}&amp;quot;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n[\/html]<\/p>\n<p><strong>NOTE: <\/strong>Attribute definition is exactly same as <strong>directive<\/strong>. Defining the first letter small with Camel-Case style and with HTML as all small letters with -\/minus separator.<\/p>\n<p>2. <strong>= (2-way-binding) Notation Symbol<\/strong>: If we bind property from <strong>Parent Scope<\/strong> to <strong>Child Directive Scope<\/strong> via <strong>= Notation<\/strong>, then the changes made to <strong>Parent Scope<\/strong> property will automatically reflect in <strong>Child Directive Scope<\/strong> property. And if changes are made to <strong>Child Directive Scope<\/strong> property, they will automatically reflect in <strong>Parent Scope<\/strong> property as well. So we call it a <strong>2-way-binding Notation<\/strong>.<\/p>\n<p>Defining a <strong>Isolated Scope Directive<\/strong> with <strong>twoWayBindingNotationSymbol<\/strong> named as below:<br \/>\n[js]<br \/>\nnotationSymbolsTestApp.directive(&#8216;twoWayBindingNotationSymbol&#8217;, function () {<br \/>\n    return {<br \/>\n        scope: {<br \/>\n            twoWayBindAge: &#8216;=userAge&#8217;<br \/>\n        },<br \/>\n        link: function (scope) {<br \/>\n            scope.increaseAge = function () {<br \/>\n                scope.twoWayBindAge++;<br \/>\n            };<br \/>\n        },<br \/>\n        template: &amp;quot;&amp;lt;div&amp;gt;Two Way Bind Age: {{twoWayBindAge}}&amp;lt;\/div&amp;gt; &amp;lt;input type=&#8217;button&#8217; value=&#8217;Increase Age&#8217; ng-click=&#8217;increaseAge()&#8217;&amp;gt;&amp;quot;<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>Now we have to use above <strong>twoWayBindingNotationSymbol<\/strong> <strong>directive<\/strong> as below, and we have to provide age value to user-age attribute which will be assigned to <strong>Isolate Directive Scope<\/strong> property named twoWayBindAge with the help of <strong>= Notation Symbol<\/strong>.<br \/>\n[html]<br \/>\n&amp;lt;div two-way-binding-notation-symbol user-age=&amp;quot;age&amp;quot;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n[\/html]<\/p>\n<p><strong>NOTE: <\/strong>When we are accessing <strong>Parent Scope<\/strong> property with <strong>@ Notation<\/strong> then we have to pass value of that property with the help of <strong>{{}}\/curly braces<\/strong>, to that attribute and when we are accessing <strong>Parent Scope<\/strong> property with <strong>= Notation <\/strong>then we have to  pass property directly to that attribute.<\/p>\n<p>3. <strong>&amp; (Function) Notation Symbol<\/strong>: If we want to access any method\/function into <strong>Isolated Directive Scope<\/strong> from <strong>Parent Scope<\/strong> then we have to use <strong>&amp; (Function) Notation Symbol<\/strong>. <\/p>\n<p>Defining a <strong>Isolated Scope Directive<\/strong> with <strong>expressionFunctionBindingNotationSymbol<\/strong> named as below:<br \/>\n[js]<br \/>\nnotationSymbolsTestApp.directive(&#8216;expressionFunctionBindingNotationSymbol&#8217;, function () {<br \/>\n    return {<br \/>\n        scope: {<br \/>\n            functionIncreaseAge: &#8216;&amp;amp;increaseAge&#8217;<br \/>\n        },<br \/>\n        template: &amp;quot;&amp;lt;input type=&#8217;button&#8217; value=&#8217;Increase Age&#8217; ng-click=&#8217;functionIncreaseAge()&#8217;&amp;gt;&amp;quot;<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>Now we have to use above <strong>expressionFunctionBindingNotationSymbol<\/strong> <strong>directive<\/strong> as below, and we have to provide increaseAge function to increase-age attribute which will be assigned to <strong>Isolate Directive Scope<\/strong> method named functionIncreaseAge with the help of<strong> &amp; Notation symbol<\/strong>.<br \/>\n[html]<br \/>\n&amp;lt;div expression-function-binding-notation-symbol increase-age=&amp;quot;increaseAge()&amp;quot;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n[\/html]<\/p>\n<p><strong>NOTE: <\/strong>Most of the time, for sharing the function\/method between <strong>Parent Scope<\/strong> and <strong>Child Directive Scope<\/strong> we use <strong>controllers<\/strong> in <strong>Directives<\/strong>, which we will discuss in one of my upcoming blog. But <strong>&amp; (Method) Notation<\/strong> can also be used in some cases.<\/p>\n<p><strong>Note: <\/strong>You can checkout full working source code from this <a href=\"https:\/\/github.com\/AmitThakkar\/AngularDemo\" target=\"_blank\">link<\/a>.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitthakkar01<br \/>\ntwitter.com\/amit_thakkar01<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous blog we studied the scope in AngularJS(we read three types of scope in a directive, Parent Scope, Inherited Scope and Isolated Scope). Isolated Scope was completely Isolated after AngularJS-1.2 so we can not access Parent Scope directly. But sometimes we face a requirement where we want to access Parent Scope property\/methods into [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[1189,1457,1460,1300,1461],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14461"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14461"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14461\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}