我正在尝试使用@Output
指令在子组件上单击按钮时通知父组件。以下是我的代码:
父视图
<app-perito-select *ngIf="peritoSelect" (cancel)="cancelPeritoAction()"></app-perito-select>
家长控制器
...
cancelPeritoAction(){
console.log('cancel inside parent');
this.selectedAction = undefined;
}
子控制器
...
@Output() cancelAction: EventEmitter<any> = new EventEmitter<any>();
...
cancel(){
console.log('cancel inside child');
this.cancelAction.emit();
}
我遵循了this教程,看起来非常简单,但我没有达到父函数。我错过了什么?感谢。
答案 0 :(得分:4)
尝试替换
(cancel)="cancelPeritoAction()"
与
(cancelAction)="cancelPeritoAction()"
因为@Output事件的名称是cancelAction
答案 1 :(得分:2)
如果要保留(取消)作为用法,可以通过将其添加为字符串参数来重命名外部输出属性:
@Output('cancel') cancelAction: EventEmitter<any> = new EventEmitter<any>();