2

我的父组件使用 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,那么它可以正常工作。任何帮助,将不胜感激。

4

1 回答 1

1

这工作正常:

父 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);
    }
}
于 2018-05-16T10:28:29.543 回答