簡體   English   中英

如何在ngSwitch的Angular 2+中將事件從子級綁定到父級組件

[英]How to bind event from child to parent component in Angular 2+ in ngSwitch

我的父組件以ngSwitch的情況調用子組件:

<div *ngSwitchDefault>
   <app-child-comp (toggleHelp)="toggleHelp($event)"></app-child-comp>
</div>

在子組件中:

@Output() toggleHelp = new EventEmitter();
this.toggleHelp.emit(true);

並在父組件中:

toggleHelp() {
  console.log('event fired');
}

到目前為止沒有成功。 如果我在不使用ngSwitch的情況下調用該組件,那么它將正常工作。 任何幫助,將不勝感激。

這工作正常:

父HTML:

<div [ngSwitch]="valuess">
  <div *ngSwitchCase="1">11111111111111111</div>
  <div *ngSwitchDefault>
    <app-child (toggleHelp)="toggleHelp($event)">

    </app-child>
  </div>
</div>
<a (click)="changeValue()">valuess</a>

家長TS:

valuess = 1;
toggleHelp(evt) {
    console.log('event fired',evt);
}
changeValue(){
    this.valuess = 2;
}

子HTML:

 <a (click)="toggleHelpCall()">toggleHelp</a>

兒童TS:

export class ChildComponent implements OnInit {
    @Output() toggleHelp = new EventEmitter();
    constructor() { }

    ngOnInit() {
    }
    toggleHelpCall() {
        this.toggleHelp.emit(true);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM