2016-11-19 33 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> 

不要忘了默認類型

相關問題