forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.Array.add.html
72 lines (66 loc) · 3.43 KB
/
angular.Array.add.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
<h1>angular.Array.add</h1>
<div class="angular-array-add"><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><code>add</code> is a function similar to JavaScript's <code>Array#push</code> method, in that it appends a new
element to an array. The difference is that the value being added is optional and defaults to
an empty object.</p>
<p>Note: this function is used to augment the <code>Array</code> type in angular expressions. See
<a href="#!angular.Array"><code>angular.Array</code></a> for more info.</p></div>
<h2>Usage</h2>
<div class="usage"><div ng:non-bindable=""><pre class="brush: js; html-script: true;">angular.Array.add(array[, value]);</pre>
</div>
<h3>Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">array – {Array} – </code>
<p>The array expand.</p></li>
<li><code ng:non-bindable="">value<i>(optional={})</i> – {*} – </code>
<p>The value to be added.</p></li>
</ul>
<h3>Returns</h3>
<div class="returns"><code ng:non-bindable="">{Array}</code>
– <p>The expanded array.</p></div>
</div>
<h2>Example</h2>
<div class="example"><p>This example shows how an initially empty array can be filled with objects created from user
input via the <code>$add</code> method.</p><doc:example>
<doc:source>
[<a href="" ng:click="people.$add()">add empty</a>]
[<a href="" ng:click="people.$add({name:'John', sex:'male'})">add 'John'</a>]
[<a href="" ng:click="people.$add({name:'Mary', sex:'female'})">add 'Mary'</a>]
<ul ng:init="people=[]">
<li ng:repeat="person in people">
<input name="person.name">
<select name="person.sex">
<option value="">--chose one--</option>
<option>male</option>
<option>female</option>
</select>
[<a href="" ng:click="people.$remove(person)">X</a>]
</li>
</ul>
<pre>people = {{people}}</pre>
</doc:source>
<doc:scenario>
beforeEach(function() {
expect(binding('people')).toBe('people = []');
});
it('should create an empty record when "add empty" is clicked', function() {
element('.doc-example a:contains("add empty")').click();
expect(binding('people')).toBe('people = [{\n "name":"",\n "sex":null}]');
});
it('should create a "John" record when "add \'John\'" is clicked', function() {
element('.doc-example a:contains("add \'John\'")').click();
expect(binding('people')).toBe('people = [{\n "name":"John",\n "sex":"male"}]');
});
it('should create a "Mary" record when "add \'Mary\'" is clicked', function() {
element('.doc-example a:contains("add \'Mary\'")').click();
expect(binding('people')).toBe('people = [{\n "name":"Mary",\n "sex":"female"}]');
});
it('should delete a record when "X" is clicked', function() {
element('.doc-example a:contains("add empty")').click();
element('.doc-example li a:contains("X"):first').click();
expect(binding('people')).toBe('people = []');
});
</doc:scenario>
</doc:example></div>
</div>