In my project I'm loading some elements dynamically.But I'm not able to generate click events for that elements.
import {Component, DynamicComponentLoader, Injector} from 'angular2/core';
@Component({
selector: 'my-app',
providers: [],
template: `
<button (click)="form()">
View Form
</button>
<div id="form" [innerHTML]="">
Welcome..! Here form component will be loaded.
</div>
`,
directives: []
})
export class App {
private strForMarkets = "";
private sbForMarkets: Array<string> = [];
constructor(public dcl:DynamicComponentLoader, public _injector:Injector) {
}
form() {
var s = ["a", "s", "d", "p"];
this.sbForMarkets.push("<div><ul>")
for (var index = 0; index < s.length; index++) {
var element = s[index];
this.sbForMarkets.push("<li class='OH liDataLeftFilterMarketsContainer'>");
this.sbForMarkets.push("<div class='FL sprite_icon expand-icon-market'></div>");
this.sbForMarkets.push("<div class='expand-text-div CP FT14 LH15' (click)='chill($event)' >" + element + "</div>");
this.sbForMarkets.push("</li>");
}
this.sbForMarkets.push("</ul></div>");
this.strForMarkets = this.sbForMarkets.join("");
}
chill() {
console.log("Dude... Chill ");
}
}
ps I have tried with dynamic component loader but it works if all html in template only.
Angular2 doesn't process HTML added dynamically using [innerHTML]="..."
or similar in any way (click)
is just ignored and added as-is.
If possible wrap the HTML in a component and use ViewContainerRef.createComponent()
instead. For an example see Angular 2 dynamic tabs with user-click chosen components
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.