X Tutup
Skip to content

Commit 6207b1a

Browse files
committed
feat(ngFor): support a custom template
Part of #1989 Closes #4637
1 parent a8c34ae commit 6207b1a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {isPresent, isBlank} from 'angular2/src/core/facade/lang';
3939
* - `<li template="ng-for #item of items; #i = index">...</li>`
4040
* - `<template ng-for #item [ng-for-of]="items" #i="index"><li>...</li></template>`
4141
*/
42-
@Directive({selector: '[ng-for][ng-for-of]', inputs: ['ngForOf']})
42+
@Directive({selector: '[ng-for][ng-for-of]', inputs: ['ngForOf', 'ngForTemplate']})
4343
export class NgFor implements DoCheck {
4444
_ngForOf: any;
4545
private _differ: IterableDiffer;
@@ -54,6 +54,8 @@ export class NgFor implements DoCheck {
5454
}
5555
}
5656

57+
set ngForTemplate(value: TemplateRef) { this._templateRef = value; }
58+
5759
doCheck() {
5860
if (isPresent(this._differ)) {
5961
var changes = this._differ.diff(this._ngForOf);

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515

1616
import {ListWrapper} from 'angular2/src/core/facade/collection';
1717

18-
import {Component, View} from 'angular2/angular2';
18+
import {Component, View, TemplateRef, ContentChild} from 'angular2/angular2';
1919

2020
import {NgFor} from 'angular2/src/core/directives/ng_for';
2121

@@ -309,6 +309,25 @@ export function main() {
309309
});
310310
}));
311311

312+
it('should allow to use a custom template',
313+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
314+
tcb.overrideTemplate(
315+
TestComponent,
316+
'<ul><template ng-for [ng-for-of]="items" [ng-for-template]="contentTpl"></template></ul>')
317+
.overrideTemplate(
318+
ComponentUsingTestComponent,
319+
'<test-cmp><li template="#item #i=index">{{i}}: {{item}};</li></test-cmp>')
320+
.createAsync(ComponentUsingTestComponent)
321+
.then((rootTC) => {
322+
var testComponent = rootTC.debugElement.componentViewChildren[0];
323+
testComponent.componentInstance.items = ['a', 'b', 'c'];
324+
rootTC.detectChanges();
325+
expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;');
326+
327+
async.done();
328+
});
329+
}));
330+
312331
});
313332
}
314333

@@ -319,6 +338,14 @@ class Foo {
319338
@Component({selector: 'test-cmp'})
320339
@View({directives: [NgFor]})
321340
class TestComponent {
341+
@ContentChild(TemplateRef) contentTpl: TemplateRef;
342+
items: any;
343+
constructor() { this.items = [1, 2]; }
344+
}
345+
346+
@Component({selector: 'outer-cmp'})
347+
@View({directives: [TestComponent]})
348+
class ComponentUsingTestComponent {
322349
items: any;
323350
constructor() { this.items = [1, 2]; }
324351
}

modules/angular2/test/public_api_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ var NG_API = [
648648
'NgFor',
649649
'NgFor.doCheck()',
650650
'NgFor.ngForOf=',
651+
'NgFor.ngForTemplate=',
651652
'NgForm',
652653
'NgForm.addControl()',
653654
'NgForm.addControlGroup()',

0 commit comments

Comments
 (0)
X Tutup