I have an Angular 4 app and a component, which generate some markup dynamically. And I need to bind this component's method to some element in this markup.
The only way, that I found is to generate this element with onclick
attribute. And so now my component's constructor looks like that:
constructor(private _zone: NgZone) {
window['Component'] = {
myMethod: this.myMethod.bind(this),
zone: _zone
}
}
...and the peice of my generated markup looks like that:
<button onclick="window.Component.myMethod()">I was generated dynamically after Angular compilation, so you can't use '(click)="method(params)"' on me! Haha!</button>
And it works! Almost... After clickin the button I need some event on window
to be fired (e.g. focus or blur), only then my method runs. Please, anybody, help.