X Tutup
Skip to content

Commit 2d0c8f1

Browse files
fix(NgFor): allow default templates with ng-for-template
Closes #5161
1 parent c6ad269 commit 2d0c8f1

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ export class NgFor implements DoCheck {
7373
}
7474
}
7575

76-
set ngForTemplate(value: TemplateRef) { this._templateRef = value; }
76+
set ngForTemplate(value: TemplateRef) {
77+
if (isPresent(value)) {
78+
this._templateRef = value;
79+
}
80+
}
7781

7882
doCheck() {
7983
if (isPresent(this._differ)) {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,39 @@ export function main() {
329329
});
330330
}));
331331

332+
it('should use a default template if a custom one is null',
333+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
334+
tcb.overrideTemplate(TestComponent, `<ul><template ng-for #item [ng-for-of]="items"
335+
[ng-for-template]="contentTpl" #i="index">{{i}}: {{item}};</template></ul>`)
336+
.overrideTemplate(ComponentUsingTestComponent, '<test-cmp></test-cmp>')
337+
.createAsync(ComponentUsingTestComponent)
338+
.then((fixture) => {
339+
var testComponent = fixture.debugElement.componentViewChildren[0];
340+
testComponent.componentInstance.items = ['a', 'b', 'c'];
341+
fixture.detectChanges();
342+
expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;');
343+
344+
async.done();
345+
});
346+
}));
347+
348+
it('should use a custom template when both default and a custom one are present',
349+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
350+
tcb.overrideTemplate(TestComponent, `<ul><template ng-for #item [ng-for-of]="items"
351+
[ng-for-template]="contentTpl" #i="index">{{i}}=> {{item}};</template></ul>`)
352+
.overrideTemplate(
353+
ComponentUsingTestComponent,
354+
'<test-cmp><li template="#item #i=index">{{i}}: {{item}};</li></test-cmp>')
355+
.createAsync(ComponentUsingTestComponent)
356+
.then((fixture) => {
357+
var testComponent = fixture.debugElement.componentViewChildren[0];
358+
testComponent.componentInstance.items = ['a', 'b', 'c'];
359+
fixture.detectChanges();
360+
expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;');
361+
362+
async.done();
363+
});
364+
}));
332365
});
333366
}
334367

0 commit comments

Comments
 (0)
X Tutup