Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An alternative to referencing an object property in the parent scope is to use $parent to access the primitive in the parent scope:</p> <pre><code>&lt;input ng-model="$parent.testvalue" /&gt; </code></pre> <p>ng-include creates a child scope. That scope prototypically inherits from Ctrl's parent scope. Here's how the 3 variations work: </p> <ul> <li>$parent.testvalue ties the model to the property in the parent scope</li> <li>testvalue by itself ties the model to a new property that will be created on the child scope. This property "shadows/hides" the parent scope property by the same name.</li> <li>foo.testvalue (e.g., see @dnc253's answer) also ties the model to a parent property. It works like this: Javascript doesn't see/find 'foo' in the child scope, so it looks for it in the parent scope (due to prototypical inheritance) and finds it there.</li> </ul> <p>To see what the child scope looks like, use your original fiddle, and add this code to your template somewhere:</p> <pre><code>&lt;a ng-click="showScope($event)"&gt;show scope&lt;/a&gt; </code></pre> <p>And add this code to your Ctrl:</p> <pre><code>$scope.showScope = function(e) { console.log(angular.element(e.srcElement).scope()); } </code></pre> <p>Before you type into the textbox, click the "show scope" link. In the console (I'm using Chrome), you can expand the "Child" scope and see it does not contain a testvalue property yet (which I find surprising, because I don't know how it is displaying the "initial value" in the textbox). You can expand the $parent and you'll see the testvalue property there -- a property with this name appears to only be in the parent scope at this point.</p> <p>Now, clear the console, type into the textbox, and click the "show scope" link again. You'll see that the "Child" scope now has a new testvalue property. It shadows/hides the parent property. So, things in the child scope see the child scope testvalue property, and things in the parent scope see the parent scope testvalue property.</p> <p><strong>Update:</strong> FYI, I recently wrote an extensive answer/article about scope prototypical inheritance that explains the above concepts in much more detail, with lots of pictures: <a href="https://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs/14049482#14049482">What are the nuances of scope prototypal / prototypical inheritance in AngularJS?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload