I am trying to figure out how I can add events to an element after it has been added to the DOM.
Right now, I have something like this:
import {Component} from 'angular2/core';
@Component({
selector : 'sub-child',
template : '<form [innerHTML]="html"></form>'
})
export class SubChildClass
{
private html:string;
private buttonText:string;
constructor()
{
this.buttonText = 'A new button';
this.create();
}
private create()
{
this.html = "<button (click)='new()'>" + this.buttonText + "</button>"
}
private new()
{
this.buttonText = "Text Changed";
}
}
The part that doesn't work is this:
this.html = "<button (click)='new()'>" + this.buttonText + "</button>"
Angular 2 doesn't know what to do with (click)='new()'
at this point. I am not really sure what is the right way to do this.
What I would expect is to be able to add HTML to the DOM at a later point with some corresponding events.
I haven't worked with Angular 1. But it sounds like this used to be the equivalent of $compile
in Angular 1. Some other post recommend using Dynamic Content Loader for stuff like this. But that doesn't seem like the right answer for this use case. So any help or guidance is appreciated. Thanks,