【发布时间】:2018-10-06 02:28:38
【问题描述】:
我在父模板中有子元素。孩子是动态添加的。从孩子我想调用父母功能,这增加了另一个孩子。我为此使用@Output,它可以在子静态时工作,但浏览器无法编译页面,当动态添加子时:
家长:
@ViewChild('dragdrophost', {
read: ViewContainerRef
})
viewContainerRef: ViewContainerRef;
loadDragDropComponent() {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(
DragDropComponent
);
let componentRef = this.viewContainerRef.createComponent(componentFactory);
}
父 HTML:
<div *ngIf="repair_picture" fxLayoutAlign.xs="center" fxLayoutWrap="wrap">
<!-- static works -->
<pd-drag-drop (loadComponent)="loadDragDropComponent($event)"></pd-drag-drop>
<!-- dynamic does not works-->
<ng-template #dragdrophost (loadComponent)="loadDragDropComponent($event)"></ng-template>
</div>
孩子:
export class DragDropComponent implements OnInit {
@Output() loadComponent = new EventEmitter<string>();
......
this.loadComponent.emit('loadComponent');
}
Compiler error:
Event binding loadComponent not emitted by any directive on an embedded template. Make sure that the event name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("eady shown, it is possible to dynamically load component-->
<ng-template #dragdrophost [ERROR ->](loadComponent)="loadDragDropComponent($event)"></ng-template>
【问题讨论】:
-
嗨,你能分享一下编译器错误吗?
-
@AbelValdez 上面共享的错误
标签: angular components eventemitter