@@ -17,6 +17,28 @@ import "../../core/change_detection/differs/default_iterable_differ.dart"
1717 show DefaultIterableDiffer, CollectionChangeRecord;
1818import "../../facade/exceptions.dart" show BaseException;
1919
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+
2042/**
2143 * The `NgFor` directive instantiates a template once per item from an iterable. The context for
2244 * each instantiated template inherits from the outer context with the given loop variable set
@@ -73,7 +95,7 @@ import "../../facade/exceptions.dart" show BaseException;
7395 inputs: const ["ngForTrackBy" , "ngForOf" , "ngForTemplate" ])
7496class NgFor implements DoCheck {
7597 ViewContainerRef _viewContainer;
76- TemplateRef _templateRef;
98+ TemplateRef < NgForRow > _templateRef;
7799 IterableDiffers _iterableDiffers;
78100 ChangeDetectorRef _cdr;
79101 /** @internal */
@@ -98,7 +120,7 @@ class NgFor implements DoCheck {
98120 }
99121 }
100122
101- set ngForTemplate (TemplateRef value) {
123+ set ngForTemplate (TemplateRef < NgForRow > value) {
102124 if (isPresent (value)) {
103125 this ._templateRef = value;
104126 }
@@ -132,22 +154,20 @@ class NgFor implements DoCheck {
132154 this ._perViewChange (insertTuples[i].view, insertTuples[i].record);
133155 }
134156 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;
138160 }
139161 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;
143165 });
144166 }
145167
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;
151171 }
152172
153173 List <RecordViewTuple > _bulkRemove (List <RecordViewTuple > tuples) {
@@ -159,7 +179,7 @@ class NgFor implements DoCheck {
159179 // separate moved views from removed views.
160180 if (isPresent (tuple.record.currentIndex)) {
161181 tuple.view = (this ._viewContainer.detach (tuple.record.previousIndex)
162- as EmbeddedViewRef );
182+ as EmbeddedViewRef < NgForRow > );
163183 movedTuples.add (tuple);
164184 } else {
165185 this ._viewContainer.remove (tuple.record.previousIndex);
@@ -175,19 +195,18 @@ class NgFor implements DoCheck {
175195 if (isPresent (tuple.view)) {
176196 this ._viewContainer.insert (tuple.view, tuple.record.currentIndex);
177197 } 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);
181200 }
182201 }
183202 return tuples;
184203 }
185204}
186205
187206class RecordViewTuple {
188- EmbeddedViewRef view;
207+ EmbeddedViewRef < NgForRow > view;
189208 dynamic record;
190- RecordViewTuple (dynamic record, EmbeddedViewRef view) {
209+ RecordViewTuple (dynamic record, EmbeddedViewRef < NgForRow > view) {
191210 this .record = record;
192211 this .view = view;
193212 }
0 commit comments