@@ -17,6 +17,28 @@ import "../../core/change_detection/differs/default_iterable_differ.dart"
17
17
show DefaultIterableDiffer, CollectionChangeRecord;
18
18
import "../../facade/exceptions.dart" show BaseException;
19
19
20
+ class NgForRow {
21
+ dynamic $implicit;
22
+ num index;
23
+ num count;
24
+ NgForRow (this .$implicit, this .index, this .count) {}
25
+ bool get first {
26
+ return identical (this .index, 0 );
27
+ }
28
+
29
+ bool get last {
30
+ return identical (this .index, this .count - 1 );
31
+ }
32
+
33
+ bool get even {
34
+ return identical (this .index % 2 , 0 );
35
+ }
36
+
37
+ bool get odd {
38
+ return ! this .even;
39
+ }
40
+ }
41
+
20
42
/**
21
43
* The `NgFor` directive instantiates a template once per item from an iterable. The context for
22
44
* each instantiated template inherits from the outer context with the given loop variable set
@@ -73,7 +95,7 @@ import "../../facade/exceptions.dart" show BaseException;
73
95
inputs: const ["ngForTrackBy" , "ngForOf" , "ngForTemplate" ])
74
96
class NgFor implements DoCheck {
75
97
ViewContainerRef _viewContainer;
76
- TemplateRef _templateRef;
98
+ TemplateRef < NgForRow > _templateRef;
77
99
IterableDiffers _iterableDiffers;
78
100
ChangeDetectorRef _cdr;
79
101
/** @internal */
@@ -98,7 +120,7 @@ class NgFor implements DoCheck {
98
120
}
99
121
}
100
122
101
- set ngForTemplate (TemplateRef value) {
123
+ set ngForTemplate (TemplateRef < NgForRow > value) {
102
124
if (isPresent (value)) {
103
125
this ._templateRef = value;
104
126
}
@@ -132,22 +154,20 @@ class NgFor implements DoCheck {
132
154
this ._perViewChange (insertTuples[i].view, insertTuples[i].record);
133
155
}
134
156
for (var i = 0 , ilen = this ._viewContainer.length; i < ilen; i++ ) {
135
- var viewRef = (this ._viewContainer.get (i) as EmbeddedViewRef );
136
- viewRef.setLocal ( "first" , identical (i, 0 )) ;
137
- viewRef.setLocal ( "last" , identical (i, ilen - 1 )) ;
157
+ var viewRef = (this ._viewContainer.get (i) as EmbeddedViewRef < NgForRow > );
158
+ viewRef.context.index = i ;
159
+ viewRef.context.count = ilen;
138
160
}
139
161
changes.forEachIdentityChange ((record) {
140
- var viewRef =
141
- ( this ._viewContainer. get (record.currentIndex) as EmbeddedViewRef );
142
- viewRef.setLocal ( " \ $ implicit" , record.item) ;
162
+ var viewRef = ( this ._viewContainer. get (record.currentIndex)
163
+ as EmbeddedViewRef < NgForRow > );
164
+ viewRef.context. $implicit = record.item;
143
165
});
144
166
}
145
167
146
- _perViewChange (EmbeddedViewRef view, CollectionChangeRecord record) {
147
- view.setLocal ("\$ implicit" , record.item);
148
- view.setLocal ("index" , record.currentIndex);
149
- view.setLocal ("even" , (record.currentIndex % 2 == 0 ));
150
- view.setLocal ("odd" , (record.currentIndex % 2 == 1 ));
168
+ _perViewChange (
169
+ EmbeddedViewRef <NgForRow > view, CollectionChangeRecord record) {
170
+ view.context.$implicit = record.item;
151
171
}
152
172
153
173
List <RecordViewTuple > _bulkRemove (List <RecordViewTuple > tuples) {
@@ -159,7 +179,7 @@ class NgFor implements DoCheck {
159
179
// separate moved views from removed views.
160
180
if (isPresent (tuple.record.currentIndex)) {
161
181
tuple.view = (this ._viewContainer.detach (tuple.record.previousIndex)
162
- as EmbeddedViewRef );
182
+ as EmbeddedViewRef < NgForRow > );
163
183
movedTuples.add (tuple);
164
184
} else {
165
185
this ._viewContainer.remove (tuple.record.previousIndex);
@@ -175,19 +195,18 @@ class NgFor implements DoCheck {
175
195
if (isPresent (tuple.view)) {
176
196
this ._viewContainer.insert (tuple.view, tuple.record.currentIndex);
177
197
} else {
178
- tuple.view = this
179
- ._viewContainer
180
- .createEmbeddedView (this ._templateRef, tuple.record.currentIndex);
198
+ tuple.view = this ._viewContainer.createEmbeddedView (this ._templateRef,
199
+ new NgForRow (null , null , null ), tuple.record.currentIndex);
181
200
}
182
201
}
183
202
return tuples;
184
203
}
185
204
}
186
205
187
206
class RecordViewTuple {
188
- EmbeddedViewRef view;
207
+ EmbeddedViewRef < NgForRow > view;
189
208
dynamic record;
190
- RecordViewTuple (dynamic record, EmbeddedViewRef view) {
209
+ RecordViewTuple (dynamic record, EmbeddedViewRef < NgForRow > view) {
191
210
this .record = record;
192
211
this .view = view;
193
212
}
0 commit comments