我有一个ContainerComponent包含一些孩子的容器组件ChildComponent,用*ngFor生成
ContainerComponent 模板:
<div class="child" *ngFor="let child of children">
<child [child]="child"></child>
</div>
Run Code Online (Sandbox Code Playgroud)
ChildComponent 模板:
<h2>{{child.name}}</h2>
<h2>{{child.data}}</h2>
Run Code Online (Sandbox Code Playgroud)
对于ChildComponent,我定义了一个样式表,我可以使用:host(https://angular.io/docs/ts/latest/guide/component-styles.html)访问整个组件主体
有了这个,我创建了以下样式ChildComponent:
:host
{
display: block;
width: 400px;
height: 300px;
}
Run Code Online (Sandbox Code Playgroud)
现在我想在每个ChildComponent(整个组件)上绑定(单击)事件并在ChildComponent类中捕获它.要做到这一点,我必须在某事物上设置(click)="method"属性.
但是,在谈论事件时,我不会:主持人的事情.
我不想绑定ContainerComponent.
一种可能的解决方案是将整个组件包装在div中并将事件绑定到此div,但是我正在寻找一种更优雅的方式.
我遇到过cordova RemoteControl插件,为了监听它的事件,需要注册addEventListener.
//listen for the event
document.addEventListener("remote-event", function(event) {
//do something
});
Run Code Online (Sandbox Code Playgroud)
对我来说这是一个合适的方式吗?