2
2
@name Tutorial: 4 - Two-way Data Binding
3
3
@description
4
4
5
- <h2 style="color: red">This page has not been updated for AngularJS 1.0 yet</h2>
6
-
7
5
<ul doc:tutorial-nav="4"></ul>
8
6
9
7
@@ -29,19 +27,19 @@ __`app/index.html`:__
29
27
...
30
28
<ul class="controls">
31
29
<li>
32
- Search: <input type="text" ng: model="query"/ >
30
+ Search: <input ng- model="query">
33
31
</li>
34
32
<li>
35
33
Sort by:
36
- <select ng: model="orderProp">
34
+ <select ng- model="orderProp">
37
35
<option value="name">Alphabetical</option>
38
36
<option value="age">Newest</option>
39
37
</select>
40
38
</li>
41
39
</ul>
42
40
43
41
<ul class="phones">
44
- <li ng: repeat="phone in phones.$ filter( query).$ orderBy( orderProp) ">
42
+ <li ng- repeat="phone in phones | filter: query | orderBy: orderProp">
45
43
{{phone.name}}
46
44
<p>{{phone.snippet}}</p>
47
45
</li>
@@ -54,14 +52,14 @@ We made the following changes to the `index.html` template:
54
52
* First, we added a `<select>` html element named `orderProp`, so that our users can pick from the
55
53
two provided sorting options.
56
54
57
- <img src="img/tutorial/tutorial_04-06_final .png">
55
+ <img src="img/tutorial/tutorial_04.png">
58
56
59
- * We then chained the `$ filter` method with {@link api/angular.module.ng.$filter.orderBy `$ orderBy`} method to
60
- further process the input into the repeater. `$ orderBy` is a utility method similar to {@link
61
- api/angular.module.ng.$filter.filter `$filter`}, but instead of filtering an array, it reorders it .
57
+ * We then chained the `filter` filter with {@link api/angular.module.ng.$filter.orderBy `orderBy`}
58
+ filter to further process the input into the repeater. `orderBy` is a filter that takes an input
59
+ array, copies it and reorders the copy which is then returned .
62
60
63
61
Angular creates a two way data-binding between the select element and the `orderProp` model.
64
- `orderProp` is then used as the input for the `$ orderBy` method .
62
+ `orderProp` is then used as the input for the `orderBy` filter .
65
63
66
64
As we discussed in the section about data-binding and the repeater in step 3, whenever the model
67
65
changes (for example because a user changes the order with the select drop down menu), Angular's
@@ -76,8 +74,8 @@ __`app/js/controller.js`:__
76
74
<pre>
77
75
/* App Controllers */
78
76
79
- function PhoneListCtrl() {
80
- this .phones = [{"name": "Nexus S",
77
+ function PhoneListCtrl($scope ) {
78
+ $scope .phones = [{"name": "Nexus S",
81
79
"snippet": "Fast just got faster with Nexus S.",
82
80
"age": 0},
83
81
{"name": "Motorola XOOM™ with Wi-Fi",
@@ -87,16 +85,16 @@ function PhoneListCtrl() {
87
85
"snippet": "The Next, Next Generation tablet.",
88
86
"age": 2}];
89
87
90
- this .orderProp = 'age';
88
+ $scope .orderProp = 'age';
91
89
}
92
90
</pre>
93
91
94
92
* We modified the `phones` model - the array of phones - and added an `age` property to each phone
95
93
record. This property is used to order phones by age.
96
94
97
95
* We added a line to the controller that sets the default value of `orderProp` to `age`. If we had
98
- not set the default value here, angular would have used the value of the first `<option>` element
99
- (`'name'`) when it initialized the data model .
96
+ not set the default value here, the model would stay uninitialized until our user would pick an
97
+ option from the drop down menu .
100
98
101
99
This is a good time to talk about two-way data-binding. Notice that when the app is loaded in the
102
100
browser, "Newest" is selected in the drop down menu. This is because we set `orderProp` to `'age'`
@@ -120,17 +118,18 @@ describe('PhoneCat controllers', function() {
120
118
var scope, $browser, ctrl;
121
119
122
120
beforeEach(function() {
123
- ctrl = new PhoneListCtrl();
121
+ scope = {};
122
+ ctrl = new PhoneListCtrl(scope);
124
123
});
125
124
126
125
127
126
it('should create "phones" model with 3 phones', function() {
128
- expect(ctrl .phones.length).toBe(3);
127
+ expect(scope .phones.length).toBe(3);
129
128
});
130
129
131
130
132
131
it('should set the default value of orderProp model', function() {
133
- expect(ctrl .orderProp).toBe('age');
132
+ expect(scope .orderProp).toBe('age');
134
133
});
135
134
});
136
135
});
@@ -162,13 +161,13 @@ __`test/e2e/scenarios.js`:__
162
161
// narrow the dataset to make the test assertions shorter
163
162
input('query').enter('tablet');
164
163
165
- expect(repeater('.phones li', 'Phone List').column('a ')).
164
+ expect(repeater('.phones li', 'Phone List').column('phone.name ')).
166
165
toEqual(["Motorola XOOM\u2122 with Wi-Fi",
167
166
"MOTOROLA XOOM\u2122"]);
168
167
169
168
select('orderProp').option('alphabetical');
170
169
171
- expect(repeater('.phones li', 'Phone List').column('a ')).
170
+ expect(repeater('.phones li', 'Phone List').column('phone.name ')).
172
171
toEqual(["MOTOROLA XOOM\u2122",
173
172
"Motorola XOOM\u2122 with Wi-Fi"]);
174
173
});
@@ -184,9 +183,12 @@ Angular's server}.
184
183
185
184
# Experiments
186
185
186
+ <div style="display:none">
187
+ !!!!! TODO(i): we need to fix select/option to support unknown option !!!!!
187
188
* In the `PhoneListCtrl` controller, remove the statement that sets the `orderProp` value and
188
189
you'll see that the ordering as well as the current selection in the dropdown menu will default to
189
190
"Alphabetical".
191
+ </div>
190
192
191
193
* Add an `{{orderProp}}` binding into the `index.html` template to display its current value as
192
194
text.
0 commit comments