2016-07-20 30 views
-2

您好,我收到一個錯誤,因爲我正在學習angular2。我決定去更換父組件中的子組件。子組件A和B都將使用內置的ngSwitch替換對方。但我抱怨說它不是綁定到child-componentA和child-componentB的本地屬性。它似乎無法將屬性綁定到兩個子組件。但是下面我遇到了一個可行的解決方案,但不明白後者爲什麼起作用。我遵循了angular2的教程。有人能解釋爲什麼提供的第一個失敗和第二個解決方案有效謝謝。瞭解angular2中的NgSwitch

parent.html

<div class= "col-md-4" [ngSwitch]="componentNumber"> 
    <child-componentA *ngSwitchCase="1"> </child-componentA> 
    <child-componentB *ngSwitchCase="2"> </child-componentB> 
</div> 

父component.ts文件:

import {Component} from '@angular/core'; 
//metadata tags defined but didn't need to include 

export class Parent { 

    componentNumber: number = 1; 

    ChangeComponent(value: number) { 
     this.componentNumber = value; 
    } 
} 

下面的代碼解決了我的問題,但我不理解爲什麼上面沒有通過上面的代碼,我跟着來自anuglar文檔的示例

使用以下代碼解決的問題:

<div class="col-md-4" [ngSwitch]="componentNumber"> 
    <template> 
     <child-componentA [ngSwitchWhen]="1"></child-componentA> 
    </template> 
    <template> 
     <child-componentB [ngSwitchWhen]="2"></child-componentB> 
    </template> 
    </div> 
+0

所以你的問題是,爲什麼使用了錯誤的語法沒有工作?我看不到'* ngSwitch'應該如何工作。 https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html – rinukkusu

+0

@rinukkusu抱歉意味着ngSwitchCase,但它不起作用 – user3497437

+0

@rinukkusu * ngSwitchCase在我使用它時不起作用 – user3497437

回答

0

如果您使用的是最新版本的RC4,檢查這些文件,以NgSwitch

+0

* ngSwitchCase不適用於我 – user3497437