forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_guide.scopes.understanding_scopes.html
62 lines (52 loc) · 3.05 KB
/
dev_guide.scopes.understanding_scopes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<h1>Developer Guide: Scopes: Understanding Scopes</h1>
<div class="developer-guide-scopes-understanding-scopes"><p>Angular automatically creates a root scope during initialization, and attaches it to the page's
root DOM element (usually <code><html></code>). The root scope object, along with any of its child scope
objects, serves as the infrastructure on which your data model is built. The data model (JavaScript
objects, arrays, or primitives) is attached to angular scope properties. Angular binds the property
values to the DOM where bindings are specified in the template. Angular attaches any controller
functions you have created to their respective scope objects.</p>
<p><img src="img/guide/simple_scope_final.png"></p>
<p>Angular scopes can be nested, so a child scope has a parent scope upstream in the DOM. When you
display an angular expression in the view, angular walks the DOM tree looking in the closest
attached scope object for the specified data. If it doesn't find the data in the closest attached
scope, it looks further up the scope hierarchy until it finds the data.</p>
<p>A child scope object inherits properties from its parents. For example, in the following snippet of
code, observe how the value of <code>name</code> changes, based on the HTML element it is displayed in:</p><doc:example>
<pre class="doc-source">
<ul ng:init="name='Hank'; names=['Igor', 'Misko', 'Gail', 'Kai']">
<li ng:repeat="name in names">
Name = {{name}}!
</li>
</ul>
<pre>Name={{name}}</pre>
</pre>
<pre class="doc-scenario">
it('should override the name property', function() {
expect(using('.doc-example-live').repeater('li').row(0)).
toEqual(['Igor']);
expect(using('.doc-example-live').repeater('li').row(1)).
toEqual(['Misko']);
expect(using('.doc-example-live').repeater('li').row(2)).
toEqual(['Gail']);
expect(using('.doc-example-live').repeater('li').row(3)).
toEqual(['Kai']);
expect(using('.doc-example-live').element('pre').text()).
toBe('Name=Hank');
});
</pre>
</doc:example><p>The angular <a href="#!/api/angular.widget.@ng:repeat"><code>ng:repeat</code></a> directive creates a new scope for each
element that it repeats (in this example the elements are list items). In the <code><ul></code> element, we
initialized <code>name</code> to "Hank", and we created an array called <code>names</code> to use as the data source for
the list items. In each <code><li></code> element, <code>name</code> is overridden. Outside of the <code><li></code> repeater, the
original value of <code>name</code> is displayed.</p>
<p>The following illustration shows the DOM and angular scopes for the example above:</p>
<p><img src="img/guide/dom_scope_final.png"></p>
<h3>Related Topics</h3>
<ul>
<li><a href="#!/guide/dev_guide.scopes">Angular Scope Objects</a></li>
<li><a href="#!/guide/dev_guide.scopes.internals">Scopes Internals</a></li>
</ul>
<h3>Related API</h3>
<ul>
<li><a href="#!/api/angular.scope"><code>Angular Scope API</code></a></li>
</ul></div>