2

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?

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks u, this is the solution that i was looking for! :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.