2016-11-10 157 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 
} 

}}

回答