내 프로젝트에서 일부 요소를 동적으로로드하고 있습니다. 그러나 요소에 대한 클릭 이벤트를 생성 할 수 없습니다.동적 구성 요소 클릭 이벤트 바인딩 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()'코드는 어떻게 생겼습니까? 'DynamicComponentLoader'는 대신에'ViewContainerRef.createComponent()'를 사용합니다. 예를 보려면 http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 –