无法捕获 Angular 4 中子组件发出的事件
Unable to catch event emitted from child component in Angular 4
很抱歉我的新手问题,我正在尝试使用 @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
但我只完成了子编辑
您实际上需要使用 (eventName)
符号绑定到子事件:
<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="doneEditingAttachment($event)"></app-new-attachment>
根据您需要的 commnet,
<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="submit()"></app-new-attachment>
很抱歉我的新手问题,我正在尝试使用 @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
但我只完成了子编辑
您实际上需要使用 (eventName)
符号绑定到子事件:
<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="doneEditingAttachment($event)"></app-new-attachment>
根据您需要的 commnet,
<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="submit()"></app-new-attachment>