如何从Angular2中的父指令监听子事件 [英] How to listen for child event from parent directive in Angular2

查看:21
本文介绍了如何从Angular2中的父指令监听子事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由指令 'MyDirective' 包装的子组件 'my-component',我需要从子组件向父指令发出一个事件,我知道如何使用 @Output 为父组件执行此操作,但是指令没有运气.

<我的组件></我的组件>

有什么想法吗?

解决方案

一种方法是使用模板变量并在事件处理程序中引用它:

<my-component (someEvent)="mydir.someMethod()"></my-component>

I have a child component 'my-component' which is wrapped by a Directive 'MyDirective' , I need to emit an event to parent directive from the child component, I know how to do it for parent component using @Output but no luck with directive.

<div myDirective >
    <my-component></my-component>
</div>

Any idea?

解决方案

One approach is to use a template variable and reference it in an event handler:

<div myDirective #mydir>
    <my-component (someEvent)="mydir.someMethod()"></my-component>
</div>

这篇关于如何从Angular2中的父指令监听子事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆