<div [ngSwitch]="contactService.contact.cat">
<div *ngSwitchWhen="'person'">
<persons-edit [primary]="true"></persons-edit>
</div>
<div *ngSwitchWhen="'business'">
<businesses-edit [primary]="true"></businesses-edit>
</div>
<div *ngSwitchWhen="'place'">
<places-edit [primary]="true"></places-edit>
</div>
</div>
這是我在我的應用程序中使用的。我定義了三個組件,並使用開關顯示我想要的組件。我從服務中獲取選擇,但可以從根組件獲取。事情是這樣的:
@Component {
selector: 'dynamic-component',
....
}
export class DynamicComponent{
@Input selectedcomponent : string;
}
在模板然後使用它:
<dynamic-component [selectedcomponent]="person"></dynamic-component>
而不是使用的 「selectedcomponent」 的服務,開關和。
一種可能的方法http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 –