Skip to content

Commit a15b679

Browse files
committed
feat(ng_for): Add Even and Odd variables to ng_for
Add even and odd local variables to ng_for to allow developers to style table rows differently and other features. Closes angular#4181
1 parent db09865 commit a15b679

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

modules/angular2/src/core/directives/ng_for.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export class NgFor implements DoCheck {
9090
private _perViewChange(view, record) {
9191
view.setLocal('\$implicit', record.item);
9292
view.setLocal('index', record.currentIndex);
93+
view.setLocal('even', (record.currentIndex % 2 == 0));
94+
view.setLocal('odd', (record.currentIndex % 2 == 1));
9395
}
9496

9597
static bulkRemove(tuples: RecordViewTuple[], viewContainer: ViewContainerRef): RecordViewTuple[] {

modules/angular2/test/core/directives/ng_for_spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,44 @@ export function main() {
271271
});
272272
}));
273273

274+
it('should display even items correctly',
275+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
276+
var template =
277+
'<div><copy-me template="ng-for: var item of items; var isEven=even">{{isEven.toString()}}</copy-me></div>';
278+
279+
tcb.overrideTemplate(TestComponent, template)
280+
.createAsync(TestComponent)
281+
.then((rootTC) => {
282+
rootTC.debugElement.componentInstance.items = [0, 1, 2];
283+
rootTC.detectChanges();
284+
expect(rootTC.debugElement.nativeElement).toHaveText('truefalsetrue');
285+
286+
rootTC.debugElement.componentInstance.items = [2, 1];
287+
rootTC.detectChanges();
288+
expect(rootTC.debugElement.nativeElement).toHaveText('truefalse');
289+
async.done();
290+
});
291+
}));
292+
293+
it('should display odd items correctly',
294+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
295+
var template =
296+
'<div><copy-me template="ng-for: var item of items; var isOdd=odd">{{isOdd.toString()}}</copy-me></div>';
297+
298+
tcb.overrideTemplate(TestComponent, template)
299+
.createAsync(TestComponent)
300+
.then((rootTC) => {
301+
rootTC.debugElement.componentInstance.items = [0, 1, 2, 3];
302+
rootTC.detectChanges();
303+
expect(rootTC.debugElement.nativeElement).toHaveText('falsetruefalsetrue');
304+
305+
rootTC.debugElement.componentInstance.items = [2, 1];
306+
rootTC.detectChanges();
307+
expect(rootTC.debugElement.nativeElement).toHaveText('falsetrue');
308+
async.done();
309+
});
310+
}));
311+
274312
});
275313
}
276314

0 commit comments

Comments
 (0)