我使用了Angular2动态组件加载器 . 一切都运作良好 .
我动态加载的组件发出事件,但我无法在任何地方捕获它 .
家长代码:
this.dcl.loadAsRoot(SomeComponent, "#dynamiccomponenthere", this.injector)
父模板:
<div id="dynamiccomponenthere" (somecustomevent)="someFunc()"></div>
儿童:
...
this.somecustomevent.emit(data)
...
***SOLUTION: (thx Gunther) ***
cmp.instance['somecustomevent'].subscribe(ev => {
this.consoleLog(ev) // run function in parent!
})
1 回答
目前
loadAsRoot()
仅用于引导根组件(AppComponent
),并且根组件不支持输入 .此评论中解释了一种解决方法https://github.com/angular/angular/issues/6370#issuecomment-193896657
另见@ContentChild is null for DynamicComponentLoader