Reputation: 586
I'm trying to create components dynamically but i want to add a click action to it and i don't know how. I was trying to do this:
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;
}
but isn't the way do that. Anybody knows how?
Upvotes: 2
Views: 3360
Reputation: 657308
You can inject the renderer and use
this.renderer.listen(widgetReference.location.nativeElement, 'click', (event) => { this.createNew(e);});
Similar to Angular2 - catch/subscribe to (click) event in dynamically added HTML
(widgetReference.location
provides the ElementRef
)
Upvotes: 3