{"id":14447,"date":"2014-06-30T11:35:36","date_gmt":"2014-06-30T06:05:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14447"},"modified":"2017-08-03T15:59:33","modified_gmt":"2017-08-03T10:29:33","slug":"scope-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/scope-in-angularjs\/","title":{"rendered":"Scope in AngularJS"},"content":{"rendered":"<p>When we work on <strong>AngularJS<\/strong>, <strong>scope<\/strong> become very important concept. So today we will <a title=\"Building Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">understand the Scope in AngularJS<\/a>. In almost every <strong>AngularJS<\/strong> <strong>Controller<\/strong> we use <strong>scope<\/strong> object which is auto injected by <strong>AngularJS<\/strong> in <strong>$scope<\/strong> named parameter. And that <strong>scope<\/strong> gets visible to marked HTML and its inner\/child HTML, because of which HTML can access all the properties and methods which are defined into the <strong>scope<\/strong> object. And if anything gets changed in the <strong>scope<\/strong>, it will automatically reflect into the HTML and if anything gets changed in HTML it will automatically reflect into <strong>scope<\/strong> object via <strong>$watch<\/strong> method. In-fact <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">AngularJS provides two-way-data-binding<\/a> with the help of <strong>$scope<\/strong> and <strong>$watch<\/strong> method. After reading this we might think <strong>scope<\/strong> is a very simple concept in <strong>AngularJS<\/strong>. But sorry that is not true \ud83d\ude41 . When we write <strong>directives<\/strong>, it gets confusing and complicated to understand. But don&#8217;t worry, I promise, today after reading this blog you will get to understand <strong>scope<\/strong> with <strong>directives<\/strong> and you will feel pretty confident with <strong>scope<\/strong> \ud83d\ude42 .<\/p>\n<p><strong>Note: <\/strong>I will discuss <strong>scope<\/strong> concept with <strong>AngularJS<\/strong> 1.2 version, in older version <strong>scope<\/strong> concept was a bit different.<\/p>\n<p>In a <strong>directive<\/strong>, we can use any one among these three values for <strong>scope<\/strong> parameter\/field:<\/p>\n<p>1. false &#8211; <strong>Parent Scope<\/strong><br \/>\n2. true &#8211; <strong>Inherited Scope<\/strong><br \/>\n3. Object hash\/{} &#8211; <strong>Isolated Scope<\/strong><\/p>\n<p>Lets first make a basic application with small HTML and a <strong>Controller<\/strong>(ScopeTestController) then we will understand <strong>scope<\/strong> with examples:<\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p>[html]<br \/>\n&lt;body ng-controller=&quot;ScopeTestController&quot;&gt;<br \/>\n&lt;div&gt;<br \/>\n    ScopeTestController: {{scopeName}} {{controllerField}}<br \/>\n    &lt;!&#8211; ScopeTestController: ScopeTestController controllerField &#8211;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n&lt;\/body&gt;<br \/>\n[\/html]<\/p>\n<p><strong>Controller:<\/strong><\/p>\n<p>[js]<br \/>\n\/*<br \/>\n * Defining ScopeTestController with two scope fields<br \/>\n * *\/<br \/>\nvar scopeTestApp = angular.module(&#8216;scopeTest&#8217;, []);<\/p>\n<p>function ScopeTestController($scope) {<br \/>\n    $scope.scopeName = &quot;ScopeTestController&quot;;<br \/>\n    $scope.controllerField = &quot;controllerField&quot;;<br \/>\n}<br \/>\n[\/js]<\/p>\n<p><strong>1. Scope = false:<\/strong> You might be thinking that <strong>directive<\/strong> creates inherited <strong>scope<\/strong> by default same as <strong>controller<\/strong> but that is not true. Default value of <strong>scope<\/strong> for all <strong>directives<\/strong> is false and those <strong>directives<\/strong> get same <strong>scope<\/strong> as of their parent. So whatever changes we make in this <strong>scope<\/strong>, will reflect in <strong>parent&#8217;s scope<\/strong> and changes done in <strong>parent scope<\/strong> will reflect to this <strong>scope<\/strong> as both are exact same <strong>scope object<\/strong>.<\/p>\n<p><strong>Defining a parent scope directive with parentScopeTestWithTemplate name:<\/strong><\/p>\n<p>[js]<br \/>\nscopeTestApp.directive(&#8216;parentScopeTestWithTemplate&#8217;, function () {<br \/>\n    return {<br \/>\n        \/\/ Default value of scope is false. &#8216;scope false&#8217; uses the same scope in which it is defined means Parent Scope.<\/p>\n<p>        link: function (scope) {<br \/>\n            scope.scopeName = &quot;parentScopeTestWithoutTemplate&quot;;<br \/>\n        },<br \/>\n        template: &#8216;parentScopeTestWithTemplate: {{scopeName}} {{controllerField}}&#8217;<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p><strong>Using parentScopeTestWithTemplate directive in HTML:<\/strong><\/p>\n<p>[html]<br \/>\n&lt;body ng-controller=&quot;ScopeTestController&quot;&gt;<br \/>\n&lt;div&gt;<br \/>\n    ScopeTestController: {{scopeName}} {{controllerField}} &lt;!&#8211; ScopeTestController: parentScopeTestWithoutTemplate controllerField &#8211;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;!&#8211; This uses Parent Scope, i.e the default one. So same scope will be on directive, template\/templateUrl and on HTML. &#8211;&gt;<\/p>\n<p>&lt;div parent-scope-test-without-template&gt;<br \/>\n    sameScopeTestWithoutTemplate: {{scopeName}} {{controllerField}} &lt;!&#8211; ScopeTestController: parentScopeTestWithoutTemplate controllerField &#8211;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n&lt;\/body&gt;<br \/>\n[\/html]<\/p>\n<p>You will see HTML print parentScopeTestWithTemplate for scopeName in both the places(inside the <strong>directive<\/strong> as well outside the <strong>directive<\/strong>) that is because of <strong>parent scope directive<\/strong> only, <strong>parent scope directive<\/strong> uses same <strong>parent scope<\/strong> and it updates the value of scopeName field so it is reflected in both the places. <strong>ngShow<\/strong> and <strong>ngHide<\/strong> are examples of <strong>Parent Scope Directive<\/strong>.<\/p>\n<p><strong>Note: <\/strong>If we are manipulating the <strong>scope<\/strong> then we should not make use of <strong>Parent Scope<\/strong>(false for <strong>scope<\/strong> parameter\/field) <strong>directive<\/strong> because it is a bad practice that we are modifying same <strong>scope<\/strong> object at more than one places.<\/p>\n<p><strong>2. Scope = true:<\/strong> this creates a new <strong>inherited scope<\/strong> from <strong>parent scope<\/strong>. And this <strong>scope<\/strong> gets visible in inner\/child HTML, in <strong>directive<\/strong> and in template(if we have defined). If we override any field of <strong>parent scope<\/strong> in newly created <strong>inherited scope<\/strong> then that updated value will be applied only in inner\/child HTML and template.<\/p>\n<p><strong>Defining an inherited scope directive with inheritedScopeTestWithTemplate name:<\/strong><\/p>\n<p>[js]<br \/>\nscopeTestApp.directive(&#8216;inheritedScopeTestWithTemplate&#8217;, function () {<br \/>\n    return {<br \/>\n        \/\/ &#8216;scope true&#8217; creates a new inherited Scope.<\/p>\n<p>        scope: true,<br \/>\n        link: function (scope) {<br \/>\n            scope.scopeName = &quot;inheritedScopeTestWithTemplate&quot;;<br \/>\n        },<br \/>\n        template: &#8216;inheritedScopeTestWithTemplate: {{scopeName}} {{controllerField}}&#8217;<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p><strong>Using inheritedScopeTestWithTemplate directive in HTML:<\/strong><\/p>\n<p>[html]<br \/>\n&lt;body ng-controller=&quot;ScopeTestController&quot;&gt;<br \/>\n&lt;div&gt;<br \/>\n    ScopeTestController: {{scopeName}} {{controllerField}} &lt;!&#8211; ScopeTestController: ScopeTestController controllerField &#8211;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;!&#8211; inherited Scope is visible in template\/templateUrl, directive and inner HTML &#8211;&gt;<\/p>\n<p>&lt;div inherited-scope-test-without-template&gt;<br \/>\n    inheritedScopeTestWithoutTemplate: {{scopeName}} {{controllerField}} &lt;!&#8211; ScopeTestController: inheritedScopeTestWithoutTemplate controllerField &#8211;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n&lt;\/body&gt;<br \/>\n[\/html]<\/p>\n<p><strong>3. Scope = {}: <\/strong>This creates new <strong>isolated scope<\/strong>. And this <strong>scope<\/strong> is visible in <strong>directive<\/strong> and in template(if we have defined) only. <strong>Parent scope<\/strong> will not get visible in <strong>directive<\/strong> and template. Before <strong>Angular1.2<\/strong> version, <strong>isolated scope<\/strong> was visible in inner\/child HTML, but now after <strong>Angular1.2<\/strong> version, <strong>isolated<\/strong> is, as its name says, actually completely <strong>isolated<\/strong>.<\/p>\n<p>[js]<br \/>\n&lt;strong&gt;Defining an isolated scope directive with isolatedScopeTestWithTemplate name:&lt;\/strong&gt;<br \/>\nscopeTestApp.directive(&#8216;isolatedScopeTestWithTemplate&#8217;, function () {<br \/>\n    return {<br \/>\n        \/\/ &#8216;scope object hash&#8217; creates a new isolated Scope.<\/p>\n<p>        scope: {},<br \/>\n        link: function (scope) {<br \/>\n            scope.scopeName = &quot;isolatedScopeTestWithTemplate&quot;;<br \/>\n        },<br \/>\n        template: &#8216;isolatedScopeTestWithTemplate: {{scopeName}} {{controllerField}}&#8217;<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p><strong>Using isolatedScopeTestWithTemplate directive in HTML:<\/strong><\/p>\n<p>[html]<br \/>\n&lt;!&#8211; Isolated Scope get only visible in template\/templateUrl and directive only but is not visible in inner HTML &#8211;&gt;<\/p>\n<p>&lt;div isolated-scope-test-with-template&gt;<br \/>\n    isolatedScopeTestWithoutTemplate: {{scopeName}} {{controllerField}} &lt;!&#8211; ScopeTestController: isolatedScopeTestWithoutTemplate &#8211;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>You will see <strong>directive<\/strong> template HTML printed only the value of scopeName but does not print the value of controllerField, because <strong>Parent scope<\/strong> does not get visible in template and <strong>isolated scope<\/strong> has only scopeName property.<\/p>\n<p><strong>NOTE: <\/strong>If we do not provide template in <strong>isolated scope directive<\/strong> then inner\/child HTML will get <strong>parent scope<\/strong> instead of <strong>isolated scope<\/strong>, because <strong>isolated scope<\/strong> only gets visible in template HTML and in directive. So only <strong>parent scope<\/strong> properties and methods will be accessible in inner\/child HTML.<br \/>\n<strong>Parent Scope<\/strong> will not be visible into <strong>isolated scope<\/strong>, but many a times we face a requirement when we need to access <strong>parent scope<\/strong> properties\/methods into <strong>isolated scope<\/strong> then we can use <strong>notations symbols<\/strong>, which we will discuss in next blog. But we do not face this issue with <strong>parent scope<\/strong> and <strong>inherited scope<\/strong> because <strong>parent scope<\/strong> is the exactly same <strong>scope<\/strong> and <strong>inherited scope<\/strong> gets visible in inner\/child HTML.<\/p>\n<p><strong>MISCELLANEOUS:<\/strong><\/p>\n<p>1. Two or more <strong>directive<\/strong> with template\/templateUrl on same node gives error, so if try below code it gives error.<\/p>\n<p>[html]<br \/>\n&lt;!&#8211;&lt;div isolated-scope-test-with-template inherited-scope-test-with-template&gt;<br \/>\n    isolatedInheritedParentScopeTestWithoutTemplate: {{scopeName}} {{controllerField}}<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;&#8211;&gt;<br \/>\n[\/html]<\/p>\n<p>2. Suppose If we are marking two <strong>directives<\/strong>(A and B) on same HTML node\/element, and template is defined into <strong>directive<\/strong> A, then template will get <strong>scope<\/strong> of <strong>directive<\/strong> A(its own <strong>directive<\/strong>) only. And if one <strong>directive<\/strong> has <strong>inherited scope<\/strong>(suppose A is an <strong>inherited scope directive<\/strong>) and one <strong>directive<\/strong> has <strong>isolated scope<\/strong>(suppose B is an <strong>isolated scope directive<\/strong>) then <strong>inherited scope<\/strong> will be applied to inner HTML(because after <strong>AngularJS 1.2 isolate scope<\/strong> will be visible in <strong>directive<\/strong> and its own template only). And if two <strong>inherited scope<\/strong> or <strong>isolated scope directive<\/strong> are marked on same node(means either both A and B are <strong>isolated scope<\/strong> <strong>directive<\/strong> or <strong>inherited scope<\/strong> <strong>directive<\/strong>) then both will get the exactly same <strong>inherited<\/strong> or <strong>isolated scope<\/strong> respectively.<\/p>\n<p>[html]<br \/>\n&lt;strong&gt;isolated&lt;\/strong&gt; &lt;strong&gt;scope&lt;\/strong&gt; respectively.<br \/>\n&lt;div isolated-scope-test-with-template inherited-scope-test-without-template&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n&lt;div isolated-scope-test-without-template inherited-scope-test-with-template&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n&lt;div isolated-scope-test-without-template inherited-scope-test-without-template&gt;<br \/>\n    ScopeTestController: {{scopeName}} {{controllerField}}<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n&lt;div inherited-scope-test-without-template inherited-scope-test2-without-template&gt;<br \/>\n    ScopeTestController: {{scopeName}} {{controllerField}}<br \/>\n&lt;\/div&gt;<br \/>\n&lt;hr\/&gt;<br \/>\n[\/html]<\/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><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/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<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we work on AngularJS, scope become very important concept. So today we will understand the Scope in AngularJS. In almost every AngularJS Controller we use scope object which is auto injected by AngularJS in $scope named parameter. And that scope gets visible to marked HTML and its inner\/child HTML, because of which HTML can [&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":5},"categories":[1185],"tags":[3955,955,4697,1189,1458,1457,1459,1300],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14447"}],"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=14447"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14447\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}