2017-10-21 83 views
0

我希望我的頁面顯示2個選項卡,每個選項卡都將包含一個表。 由於表的結構相同,我爲表創建了一個自定義組件,並將其用作子組件。 父頁面:Angular2:在父級中使用兩個相同的子組件

<p-tabPanel> 
    <cutomComp #table1 [items]=>"variable1"</customComp> 
</<p-tabPanel> 
<p-tabPanel> 
    <cutomComp #table2 [items]="variable2"></customComp> 
</<p-tabPanel> 

我的自定義組件:

<p-dataTable [value]="itemsForTable"> 
</p-dataTable> 

ngOnChanges(changes: SimpleChanges){ 
    //init itemsForTable from items 
} 

的問題是,該表被更新(ngOnChanges叫法),只有當變量1被改變,而不是當變量2被改變。爲什麼? 另外,當變量1的ngOnChanges被執行時,表2也被更新了。 那麼問題是什麼?我希望每張桌子都是獨立的,我怎麼能做到這一點?

在此先感謝。

+0

'[items] =>「variable1」'是一個可能的罪魁禍首,如果它在你的實際代碼中 –

回答

0

問題1:

<cutomComp #table1 **[items]=>"variable1"**</customComp> 

這裏應該[項目] = 「變量1」。

問題2:

如果以上錯誤是您輸入錯誤。 正如你在這裏使用的primeng選項卡來顯示兩個表,使用一個布爾值來顯示/隱藏基於標籤點擊的數據表。這段代碼不會導致任何問題。

<p-tabPanel> 
    <cutomComp #table1 *ngIf="showTable1" [items]="variable1"</customComp> 
</<p-tabPanel> 
<p-tabPanel> 
    <cutomComp #table2 *ngIf="showTable2" [items]="variable2"></customComp> 
</<p-tabPanel> 
相關問題