我正在尝试动态创建组件,但我想向它添加一个点击操作,我不知道如何。我试图这样做:
constructor(public navCtrl: NavController, private resolver: ComponentFactoryResolver) {
this.lastSelectedResource = this.defaultImage;
}
public createNew() {
this.container.detach(0);
}
ngAfterContentInit() {
let widgetFactory = this.resolver.resolveComponentFactory(CreateComponent);
let widgetReference = this.container.createComponent(widgetFactory);
widgetReference.instance.click = this.createNew;
}
但不是那样做的。谁知道怎么做?
答案 0 :(得分:2)
您可以注入渲染器并使用
this.renderer.listen(widgetReference.location.nativeElement, 'click', (event) => { this.createNew(e);});
与Angular2 - catch/subscribe to (click) event in dynamically added HTML类似
(widgetReference.location
提供ElementRef
)