2016-09-21 116 views
3

我有以下問題: 讓我們假設我有Component A有兩個subcomponents ArAdSubcomponent Ar是遞歸創建樹,subcomponent Ad是顯示有關遞歸樹中選定節點的詳細信息的組件(subcomponent Ar)。如何使用@OutputAr中的子(子)組件向Component Ad發送選定節點?應該是@Output還是別的?Angular2遞歸組件輸出

app.component.html:

<tree [input]="fooTree" [output]="updateDetails($event)"></tree> 
<tree-details [input]="input"></tree-details> 

tree.component.html:

<tree [input]="fooTree.children"></tree> 

樹details.component.html

<div>{{input.name}}</div> 
<div>{{input.id}}</div> 

在這種情況下,我只會看到根樹的詳細信息,如何才能從其他節點(遞歸創建的一個節點)獲取信息,何時被選中?

回答

5

UPDATE

它更容易在一個演示應用查看:https://plnkr.co/edit/WaYluZyPaC0OEV0YovbC?p=preview

..

tree-component氬可以有一個@Ouput()。 您的AppComponent將使用此輸出並將選定的數據發佈到第二個子組件detail-component

app.component.html

<tree (yourOutputNameHere)="yourFunctionToReceiveAndPostSelectedData($event)"></tree> 
<details #yourDetailViewComponent></details> 

tree.component.html

<tree (yourOutputNameHere)="yourFunctionToReceiveAndPostSelectedData($event)"></tree> 

tree-component甚至可以有一個@Input()和你的模板裏面,你可以做這樣的事情:

app.component.html

<tree [detail-view-input]="yourDetailViewComponent"></tree> 
<details #yourDetailViewComponent></details> 

tree.component.html

<tree [detail-view-input]="detailViewInput"></tree> 
+0

但隨着tree.component.html什麼?不應該有任何輸出? – ulou

+0

是的,它的遞歸,不記得它.. – mxii

+0

這是我需要的exatcly。非常感謝你 :) – ulou