2017-09-21 33 views
-1

我试图使用@Output指令来通知父组件当按钮被单击子组件。以下是我的代码:Angular - @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教程,它看起来很简单,但我没有达到父功能。我错过了什么?谢谢。

回答

4

尝试

(cancelAction)="cancelPeritoAction()" 

更换

(cancel)="cancelPeritoAction()" 

因为你@Output事件的名称为cancelAction

1

你就可以把它作为你一个字符串参数重命名你的外部输出特性想保留(取消)作为用法:

@Output('cancel') cancelAction: EventEmitter<any> = new EventEmitter<any>();