【发布时间】:2016-03-05 02:12:25
【问题描述】:
我正在尝试检测子组件中的点击。在其兄弟组件中,我想清空其中的输入字段。让我用一个简短的例子来说明我的意思:
// Parent component template:
<div>
<child1-comp></child1-comp>
<child2-comp></child2-comp>
</div>
.
// child1-comp template:
<input type="text" #inputText (keyup)="0">
.
// child2-comp template:
<ul>
<li (click)="onClick(text1.value)" #text1>Test</li>
<li (click)="onClick(text2.value)" #text2>Test2</li>
</ul>
在child2-comp 中的一个点击事件之后,我想清空child1-comp 中输入字段中的值。
在 Angular 2 中可以做到这一点吗?
【问题讨论】:
-
有几个选项,查看官方食谱,章节Component Interaction 示例。
-
@Sasxa 我尝试使用来自
child2的事件发射器,它只会发出一个布尔值。在父级中,我抓取了$event并将其存储在一个变量中。然后将此变量传递给child1。我尝试使用*ngIf,但遇到了模板问题。我在正确的轨道上吗?还是我应该寻找其他方法? -
你能用你的设置创建一个plunker吗?
标签: angular