[英]Adding a click event to a dynamically created html element using angular2
[英]Angular2 fire click event on unknown html element
我想在父组件中处理x3dom的shape
html元素的click事件。 x3dom触发了onclick
事件 。 我只能通过丑陋的方式委派它(请参阅下文)。 因为Angular 2并不知道shape
是Html标签,所以我必须定义一个shape
组件? 或不?
在父组件中:
<shape (click)="doStuffInParent()" ></shape> <!-- click is not fired by x3dom -->
到目前为止的形状组件:
import {Component} from '@angular/core';
import { Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'Shape',
providers: [],
directives: [],
pipes: []
})
export class Shape {
@Output() notify: EventEmitter<string> = new EventEmitter<string>(); // wrong!
constructor() {}
}
编辑:也许我不需要该组件? X3dom而不是我触发了onclick事件。
我的另一个解决办法是,如果我可以叫我的组件的方法,从一个普通onclick
事件我问这里 。
更新我破解了一个解决方案,至少解决了我的问题:
工作演示: https : //plnkr.co/edit/qQudgi9touIFOe52m4JY?p=preview
<Shape (myClick)="doStuffInParent($event)"></Shape>
在组件代码中,
doStuffInParent(value){
console.log(value); //Angular2
}
import {Component} from '@angular/core';
import { Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'Shape',
providers: [],
directives: [],
pipes: [],
template:`<div (click)="click()">Shap Component</div>`
})
export class Shape {
@Output() myClick: EventEmitter<string> = new EventEmitter<string>();
click(){
this.myClick.next('Angular2');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.