[英]How fire an event from child component to parent component?
public test(){
//some code.....
}
<app-child (eventfire)="test($event)"></app-child>
@Output() eventfire= new EventEmitter<string>();
calltoParent(): void {
debugger;
this.eventfire.next();
}
<button mat-raised-button (click) = calltoParent($event);>Click Here<button>//This already show in grid
您可以使用output
和EventEmitter
来实现此目的。 更新以下代码:
在parent.component.html
中,添加子选择器及其触发的事件:
<app-child (clicked)="test()"></app-child>
在parent.component.ts
添加test
方法:
test(){
alert('parent function called from child!')
}
现在在child.component.ts
添加以下代码:
@Output() clicked = new EventEmitter();
callParent(){
this.clicked.emit()
}
单击按钮时触发callParent
方法:
<button (click)="callParent()"> click </button>
这是工作示例: StackBlitz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.