Angular2 渲染模板
Angular2 render template
我有两个组件,ComponentA 使用ComponentB。 ComponentB 是一个 table,它可以检索数据、选项和定义 table 内容的模板。
我希望 ComponentB 成为一个组件来呈现不同的 tables。我不知道如何在 Angular2 v2.1.0 中呈现模板。我发现像 DynamicComponentLoader 和 ComponentResolver 这样的东西都已经过时了。 Angular2 2.1.0 提供了哪些组件来渲染模板?
组件A:
@Component({
'template':<my-table [template]="template" [options]="options" [data]="data"></mytable>
)}
export class ViewA{
template:string;
ngOnInit(){
this.template = `
<tr
*ngFor="let item of items">
<td>{{item.name}}</td>
</tr>
`
}
}
组件 B:
@Component({
selector: 'my-table',
'template':`
<table>
<thead>
<th *ngFor="let conf of config.columns">
{{conf.title}}
</th>
</thead>
<tbody #tBody>
</tbody>
</table>
`
)}
export class ViewB{
@Input('template') template:string;
@Input('data') data:MyData;
@Input('options') options:MyOptions;
constructor(){
//todo: create template element, append template element into the tbody element
}
}
}
我刚看了 Rob Wormald 就这个主题制作的视频。它有点长,但很好地了解了如何自己做。
我有两个组件,ComponentA 使用ComponentB。 ComponentB 是一个 table,它可以检索数据、选项和定义 table 内容的模板。 我希望 ComponentB 成为一个组件来呈现不同的 tables。我不知道如何在 Angular2 v2.1.0 中呈现模板。我发现像 DynamicComponentLoader 和 ComponentResolver 这样的东西都已经过时了。 Angular2 2.1.0 提供了哪些组件来渲染模板?
组件A:
@Component({
'template':<my-table [template]="template" [options]="options" [data]="data"></mytable>
)}
export class ViewA{
template:string;
ngOnInit(){
this.template = `
<tr
*ngFor="let item of items">
<td>{{item.name}}</td>
</tr>
`
}
}
组件 B:
@Component({
selector: 'my-table',
'template':`
<table>
<thead>
<th *ngFor="let conf of config.columns">
{{conf.title}}
</th>
</thead>
<tbody #tBody>
</tbody>
</table>
`
)}
export class ViewB{
@Input('template') template:string;
@Input('data') data:MyData;
@Input('options') options:MyOptions;
constructor(){
//todo: create template element, append template element into the tbody element
}
} }
我刚看了 Rob Wormald 就这个主题制作的视频。它有点长,但很好地了解了如何自己做。