Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emitting event from structural directive to parent component does not work

As a follow up to question: Emit event from Directive to Parent element : Angular2

It looks like when a structural directive emits an event, the parent component does not receive it.

@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>

In this case I find containerDoSomthing() never getting called.

like image 437
nesdis Avatar asked May 29 '17 06:05

nesdis


1 Answers

It's possible. The issue is that current Angular 5.2.6 still doesn't support @Output binding for structural directives if used with the sugared asterisk (*) syntax (see GitHub issue) like in the question.

To make it work you have to transform it to de-sugarized form (see here) like this:

<ng-template [appWidget]="wdg" (wdgInit)="containerDoSomthing($event)"></ng-template>
like image 109
Alexander Abakumov Avatar answered Oct 11 '22 23:10

Alexander Abakumov