【问题标题】:Angular - @Output not workingAngular - @Output 不起作用
【发布时间】:2018-03-02 23:57:16
【问题描述】:

我正在尝试使用@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 教程,看起来很简单,但我没有达到父函数。我错过了什么?谢谢。

【问题讨论】:

    标签: angular angular2-directives


    【解决方案1】:

    尝试替换

    (cancel)="cancelPeritoAction()"
    

    (cancelAction)="cancelPeritoAction()"
    

    因为您的@Output 事件的名称是cancelAction

    【讨论】:

      【解决方案2】:

      如果您想保留(取消)作为用法,您可以通过将其添加为字符串参数来重命名外部输出属性:

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

      【讨论】:

        猜你喜欢
        • 2017-12-13
        • 1970-01-01
        • 2018-04-18
        • 2021-09-02
        • 2017-12-14
        • 2021-12-30
        • 1970-01-01
        • 2017-10-03
        • 1970-01-01
        相关资源
        最近更新 更多