2016-11-19 84 views
1

我有两个角2组件,<future-todos><past-todos>。我可以以某种方式动态加载它们,如下所示?是否可以通过动态更改其选择器名称加载Angular 2组件?

component.ts

if (condition) { 
    list = 'future'; 
} else { 
    list = 'past'; 
} 

component.html

<{{list}}-todos></{{list}}-todos> 

我的实际情况更复杂,我省略了大量的代码,以简化表示本实例我需要。

+0

不要错过这个[我如何使用/创建动态模板来编译动态组件与Angular 2.0?](http://stackoverflow.com/q/38888008/1679310) –

回答

1

简短的回答:第

NgSwitch在这种情况下使用:

HTML:

<div [ngSwitch]="list"> 
    <div *ngSwitchCase="future"> 
     <future-todos></future-todos> 
    </div> 
    <div *ngSwitchCase="past"> 
     <past-todos></past-todos> 
    </div> 
    <div *ngSwitchDefault>Unrecognized list type</div> 
</div> 

不要忘了默认类型

相关问题