2016-11-10 240 views
2

我有两个组件,ComponentA使用ComponentB。 ComponentB是一个表格,可以检索数据,选项和定义表格内容的模板。 我希望ComponentB是一个组件来呈现不同的表格。我不知道如何在Angular2 v2.1.0中呈现模板。我发现了像DynamicComponentLoader和ComponentResolver这些都过时的东西。 Angular2 2.1.0提供了哪些组件来呈现模板?Angular2渲染模板

ComponentA:

@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> 
     ` 
    } 
} 

以componentB:

@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 
} 

}}

回答