2016-09-21 66 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