在我的項目中,我正在動態加載一些元素。但我無法爲這些元素生成單擊事件。動態組件單擊事件綁定Angular2
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 ");
}
}
p.s.我已經嘗試過使用動態組件加載器,但只有在模板中的所有HTML都可以使用。
你在哪裏添加它? 'this.dcl.loadXxx()'代碼是怎麼樣的?改爲使用'ViewContainerRef.createComponent()'代替'DynamicComponentLoader'。有關示例,請參閱http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 –