javascript - 无法捕获从 Angular 4 中的子组件发出的事件

标签 javascript angular typescript angular2-components

对于我的新手问题,我很抱歉,我正在尝试使用 @Output EventEmitter 将事件从子组件发送到父组件。我无法在我的父组件中捕获该事件。

子组件

@Component({
  selector: 'app-new-attachment',
  templateUrl: './new-attachment.component.html',
  styleUrls: ['./new-attachment.component.css']
})
class NewAttachmentComponent {
  @Input('attachment') attachment: any;
  @Output() doneEditingAttachment:EventEmitter<boolean> = new EventEmitter<boolean>();

  submit() {
    console.log('done editing child');
    this.doneEditingAttachment.emit(true);
  }
}

父组件

@Component({
  selector: 'app-new-article',
  templateUrl: './new-article.component.html',
  styleUrls: ['./new-article.component.css']
})
class NewArticleComponent {
   newAttachment: any = {};
   doneEditingAttachment($event) {
     console.log('done editing parent ', $event);
   }
}

我希望有

done editing child

还有一个

done editing parent

但我只完成了对 child 的编辑

最佳答案

您实际上需要使用 (eventName) 符号绑定(bind)到子事件:

<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="doneEditingAttachment($event)"></app-new-attachment>

关于javascript - 无法捕获从 Angular 4 中的子组件发出的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569130/

相关文章:

Angular 如何在 canActive 函数中获取 header 值?

javascript - 谷歌地图在 Ionic 2 应用程序中不起作用

javascript - 循环最多 x 个数字,或更少

javascript - 使用 AJAX 重定向到 Rails 路径

angular - 函数在 5000 毫秒后超时 - Angular 4 - Protractor 和 cucumber

javascript - 如何在标记上添加不对称脉冲动画

angular - Web OTP API Typescript Typings 问题 - TypeScript 中缺少类型

javascript - 如何在指令模板中使用动态 ng-show 值?

javascript - 更改从 HTML 页面复制到剪贴板的文本颜色

Angular - 错误引用错误 : config is not defined