Angular 如何使用EventEmitter输出和订阅从子函数调用父函数
我需要使用事件从modalComponent刷新表 子组件中的Angular 如何使用EventEmitter输出和订阅从子函数调用父函数,angular,modal-dialog,ngx-bootstrap,ngx-datatable,Angular,Modal Dialog,Ngx Bootstrap,Ngx Datatable,我需要使用事件从modalComponent刷新表 子组件中的 @Output() callrefresh = new EventEmitter<string>(); this.service.save(this.car).subscribe( (result) => { this.closeModal(); this.callrefresh .emit('I want to call a function');
@Output() callrefresh = new EventEmitter<string>();
this.service.save(this.car).subscribe(
(result) => {
this.closeModal();
this.callrefresh .emit('I want to call a function');
},
refreshTable(){
this.tableRefresh();
}
tableRefresh(
page?: Page
): void {
this.subscriptions.push(
this.service
.findAlldata(
this.sort.dir,
this.page.pageNumber,
this.page.size,
this.sort.prop
)
.subscribe({
next: (data: any) => {
all paged data logic
);
},
error: (error: any) => {
console.log(error);
},
complete: () => {
console.log('complete');
},
})
);
}
在父组件中,我想激活此功能
refreshTable(){
this.tableRefresh();
}
有什么帮助吗?举个例子就好了
在父组件中有一个表,在子组件中有一个模态。当我在子组件中保存数据行时,我希望在父组件中激活刷新功能
例如,在子组件中
@Output() callrefresh = new EventEmitter<string>();
this.service.save(this.car).subscribe(
(result) => {
this.closeModal();
this.callrefresh .emit('I want to call a function');
},
refreshTable(){
this.tableRefresh();
}
tableRefresh(
page?: Page
): void {
this.subscriptions.push(
this.service
.findAlldata(
this.sort.dir,
this.page.pageNumber,
this.page.size,
this.sort.prop
)
.subscribe({
next: (data: any) => {
all paged data logic
);
},
error: (error: any) => {
console.log(error);
},
complete: () => {
console.log('complete');
},
})
);
}
在父组件中
@Output() callrefresh = new EventEmitter<string>();
this.service.save(this.car).subscribe(
(result) => {
this.closeModal();
this.callrefresh .emit('I want to call a function');
},
refreshTable(){
this.tableRefresh();
}
tableRefresh(
page?: Page
): void {
this.subscriptions.push(
this.service
.findAlldata(
this.sort.dir,
this.page.pageNumber,
this.page.size,
this.sort.prop
)
.subscribe({
next: (data: any) => {
all paged data logic
);
},
error: (error: any) => {
console.log(error);
},
complete: () => {
console.log('complete');
},
})
);
}
您只需使用由输出触发的事件。发出时,将调用refreshTable()
<child (callrefresh)="refreshTable()"></child>
能否请您添加更多详细信息,您的意图是什么,您希望从哪个组件访问什么,并为您希望交互的两个组件添加更多代码。或者一个stackblitz也可以。请发布相关的模板。请确保您的问题中包含一个“不工作”,您能给我一个完整的示例吗?这正是输出的工作原理,因此,如果它不工作,那是因为您的refreshTable不工作。使用subscribe,child hashtag会断开表viewChild不存在,因为它只是一个示例,您需要使用实际的组件Selectories,我使用了我的组件选择器,但不起作用