{"id":10,"date":"2016-02-08T03:48:27","date_gmt":"2016-02-08T03:48:27","guid":{"rendered":"http:\/\/165.225.137.195\/blog\/?p=4"},"modified":"2016-02-08T03:48:27","modified_gmt":"2016-02-08T03:48:27","slug":"directives-scope-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2016\/02\/08\/directives-scope-in-angularjs\/","title":{"rendered":"Directives scope in AngularJS"},"content":{"rendered":"<p>Scope is <span class=\"c2\">context where the model is stored so that controllers, directives and expressions can access it.\u00a0<\/span><span class=\"c2\">By default, each directive inherits its parent\u2019s scope, which is passed to it in the link\u00a0<\/span><span class=\"c2\">function. below three options are described scope creation in angularjs,<\/span><\/p>\n<p class=\"c1\"><span class=\"c2 c3\">Scope:<\/span><span class=\"c2 c3 c6\">false <\/span><span class=\"c2 c6\">(default) <\/span><span class=\"c2\">which basically tells AngularJS that the directive scope is\u00a0<\/span><span class=\"c2\">the same as the parent scope.<br \/>\n<\/span><span class=\"c2 c3\">Example<\/span><span class=\"c2\">: If scope:false means it will simply return same address location of parent scope.<br \/>\n<\/span><span class=\"c2\">working <\/span><span class=\"c5\"><a class=\"c4\" href=\"https:\/\/www.google.com\/url?q=http:\/\/jsfiddle.net\/9Ymvt\/2829\/&amp;sa=D&amp;ust=1454905622308000&amp;usg=AFQjCNECzXs_bMZofMLVFJx8V_CDrSHXXA\">Fiddle<\/a><\/span><span class=\"c2\">.<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nreturn {\n    restrict: 'A',\n      scope:false,\n    template: '',\n    link: function(scope, element, attrs) {\n        console.log(scope);\n        scope.data = scope.data + &quot; + directive postfix&quot;;\n        child = scope;\n    }\n<\/pre>\n<p class=\"c1\"><span class=\"c2 c3\">Result<\/span><span class=\"c2\">:<\/span><\/p>\n<p class=\"c1\">\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-7\" src=\"http:\/\/ec2-34-220-211-201.us-west-2.compute.amazonaws.com\/blog\/wp-content\/uploads\/2016\/02\/image01.png\" alt=\"image01\" width=\"300\" height=\"160\" \/><\/p>\n<p class=\"c1\"><span class=\"c2 c3 c6\">Scope:true<\/span><span class=\"c2\">\u00a0creates new scope object but never override parent properties. The directive thus gets access to all the variables and functions from the parent scope, but any modifications it makes are not available in the parent.<br \/>\n<\/span><span class=\"c2 c3\">Example<\/span><span class=\"c2\">: create a child scope prototypically inherited with scope: true.<br \/>\n<\/span><span class=\"c2\">working <\/span><span class=\"c5\"><a class=\"c4\" href=\"https:\/\/www.google.com\/url?q=http:\/\/jsfiddle.net\/9Ymvt\/2834\/&amp;sa=D&amp;ust=1454905622316000&amp;usg=AFQjCNGv2augimzJLHQKROIPpzYtOZHPGg\">Fiddle<\/a><\/span><span class=\"c2\">.<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nreturn {\n    restrict: \u2018A\u2019,\n      scope:true,\n    template: \u201d,\n    link: function(scope, element, attrs) {\n        console.log(scope);\n        scope.data = scope.data + \u201d + directive postfix\u201d;\n        child = scope;\n    }\n<\/pre>\n<p class=\"c1\"><span class=\"c2 c3\">Result<\/span><span class=\"c2\">:<\/span><\/p>\n<p class=\"c1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6\" src=\"http:\/\/ec2-34-220-211-201.us-west-2.compute.amazonaws.com\/blog\/wp-content\/uploads\/2016\/02\/image00.png\" alt=\"image00\" width=\"403\" height=\"234\" \/><\/p>\n<p class=\"c1\"><span class=\"c2 c3 c6\">Scope:{} <\/span><span class=\"c2\">(isolated scope) This scope does not inherit anything from the parent, and any data that the parent scope needs to share with this directive needs to be passed in through HTML attributes<br \/>\n<\/span><span class=\"c2 c3\">Example<\/span><span class=\"c2\">: create an isolated scope with scope: {} then you can bind some property to parent scopes with &#8216;@&#8217;, &#8216;&amp;&#8217;, &#8216;=&#8217;.<br \/>\n<\/span><span class=\"c2\">working <\/span><span class=\"c5\"><a class=\"c4\" href=\"https:\/\/www.google.com\/url?q=http:\/\/jsfiddle.net\/9Ymvt\/2835\/&amp;sa=D&amp;ust=1454905622322000&amp;usg=AFQjCNGNGDSc0jASSOPU_8Me1pYgpWCNHw\">Fiddle<\/a><\/span><span class=\"c2\">.<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nreturn {\n    restrict: \u2018A\u2019,\n      scope:{},\n    template: \u201d,\n    link: function(scope, element, attrs) {\n        console.log(scope);\n        scope.data = scope.data + \u201d + directive postfix\u201d;\n        child = scope;\n    }\n<\/pre>\n<p class=\"c1\"><span class=\"c2 c3\">Result<\/span><span class=\"c2\">:<\/span><\/p>\n<p class=\"c1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-8\" src=\"http:\/\/ec2-34-220-211-201.us-west-2.compute.amazonaws.com\/blog\/wp-content\/uploads\/2016\/02\/image02.png\" alt=\"image02\" width=\"300\" height=\"172\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scope is context where the model is stored so that controllers, directives and expressions can access it.\u00a0By default, each directive inherits its parent\u2019s scope, which is passed to it in the link\u00a0function. below three options&hellip; <a href=\"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2016\/02\/08\/directives-scope-in-angularjs\/\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,15],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-angular","category-angular-1-4","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}