forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.directive.ng:options.html
95 lines (88 loc) · 4.04 KB
/
angular.directive.ng:options.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<h1>angular.directive.ng:options</h1>
<div class="angular-directive-ng-options"><fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<h2>Description</h2>
<div class="description"><p>Dynamically generate a list of <code><option></code> elements for a <code><select></code> element using the array
obtained by evaluating the <code>ng:options</code> expression.</p>
<p>When an item in the select menu is select, the array element represented by the selected option
will be bound to the model identified by the <code>name</code> attribute of the parent select element.</p>
<p>Optionally, a single hard-coded <code><option></code> element, with the value set to an empty string, can
be nested into the <code><select></code> element. This element will then represent <code>null</code> or "not selected"
option. See example below for demonstration.</p>
<p>Note: <code>ng:options</code> provides iterator facility for <code><option></code> element which must be used instead
of <a href="#!/api/angular.widget.@ng:repeat"><code>ng:repeat</code></a>. <code>ng:repeat</code> is not suitable for use with
<code><option></code> element because of the following reasons:</p>
<ul>
<li>value attribute of the option element that we need to bind to requires a string, but the
source of data for the iteration might be in a form of array containing objects instead of
strings</li>
<li><a href="#!/api/angular.widget.@ng:repeat"><code>ng:repeat</code></a> unrolls after the select binds causing
incorect rendering on most browsers.</li>
<li>binding to a value not in list confuses most browsers.</li>
</ul></div>
<h2>Usage</h2>
<div class="usage"><pre class="brush: js; html-script: true;"><select ng:options="comprehension">
...
</select></pre>
<h3>Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">comprehension – {comprehension_expression} – </code>
<p><em>expresion</em> <code>for</code> <em>item</em> <code>in</code> <em>array</em>.</p>
<ul>
<li><em>array</em>: an expression which evaluates to an array of objects to bind.</li>
<li><em>item</em>: local variable which will reffer to the item in the <em>array</em> during the itteration</li>
<li><em>expression</em>: The result of this expression will is <code>option</code> label. The
<code>expression</code> most likely reffers to the <em>item</em> varibale.</li>
</ul></li>
</ul>
</div>
<h2>Example</h2>
<div class="example"><doc:example>
<pre class="doc-source">
<script>
function MyCntrl(){
this.colors = [
{name:'black'},
{name:'white'},
{name:'red'},
{name:'blue'},
{name:'green'}
];
this.color = this.colors[2]; // red
}
</script>
<div ng:controller="MyCntrl">
<ul>
<li ng:repeat="color in colors">
Name: <input name="color.name"/> [<a href ng:click="colors.$remove(color)">X</a>]
</li>
<li>
[<a href ng:click="colors.push({})">add</a>]
</li>
</ul>
<hr/>
Color (null not allowed):
<select name="color" ng:options="c.name for c in colors"></select><br/>
Color (null allowed):
<select name="color" ng:options="c.name for c in colors">
<option value="">-- chose color --</option>
</select><br/>
Select <a href ng:click="color={name:'not in list'}">bogus</a>. <br/>
<hr/>
Currently selected: {{ {selected_color:color} }}
<div style="border:solid 1px black;"
ng:style="{'background-color':color.name}">
&nbsp;
</div>
</div>
</pre>
<pre class="doc-scenario">
it('should check ng:options', function(){
expect(binding('color')).toMatch('red');
select('color').option('0');
expect(binding('color')).toMatch('black');
select('color').option('');
expect(binding('color')).toMatch('null');
});
</pre>
</doc:example></div>
</div>