javascript – 从结构指令发送到父组件的事件不起作用

作为后续问题:Emit event from Directive to Parent element : Angular2

看起来当结构指令发出事件时,父组件不会接收它.

@Directive({ selector: '[appWidget]' })
export class WidgetDirective implements OnInit{
@Output() wdgInit: EventEmitter<any> = new EventEmitter();
@Input() set appWidget (wdg: any) {
    //display stuff
}
ngOnInit {
   this.wdgInit.emit();
}

widget.component.html:

  <ng-container *ngFor="let wdg of widgets">      
  <div *appTwitterWidget="wdg" >
  <ng-container>

widgetContainer.component.html:

 <app-widget [widgets]="widgetList" (wdgInit)="containerDoSomthing()"></app-widget>

在这种情况下,我发现从不调用containerDoSomthing().

解决方法:

这是可能的.问题是当前的Angular 5.2.6仍然不支持结构指令的@Output绑定,如果与问题中的含糖星号(*)语法一起使用(参见GitHub issue).

为了使它工作,你必须将它转换为去糖形式(see here),如下所示:

<ng-template [appWidget]="wdg" (wdgInit)="containerDoSomthing($event)"></ng-template>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐