Pass data to transcluded repeated element from repeating component

不打扰是莪最后的温柔 提交于 2019-12-05 16:56:09

You can use ngForTemplate like this:

@Component({
    selector: 'foo',
    template: `
        <h1>I am foo</h1>
        <div>
         <template ngFor [ngForOf]="data" [ngForTemplate]="itemTemplate"></template>
        </div>`
})
export class Foo {
    @Input() data: any[];
    @ContentChild(TemplateRef) itemTemplate: TemplateRef<any>;
}

@Component({
  selector: 'my-app',
  template: `<h1>Angular 2 Systemjs start</h1>
    <foo [data]="items">
        <template let-item>
            <div>item: {{item}}</div>
        </template>
    </foo>
 `,
  directives: [Foo],

})
export class AppComponent {
    items = [1, 2, 3, 4];
}

Plunker example

Or instead template let-item you can write:

<foo [data]="items">
   <div template="let item">item: {{item}}</div>
</foo>

Plunker example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!