简体   繁体   English

我如何在 Angular 中的对话框 window 中调用基本组件上的 function

[英]How can i call a function on a base component from a dialog window in Angular

I am looking for a way to call a function on a component.我正在寻找一种在组件上调用 function 的方法。 Here is my Setup, i have a reusable component which is my base for all data displayed in grids and includes the filter logic, chips for filters, export and the actual grid.这是我的设置,我有一个可重复使用的组件,它是网格中显示的所有数据的基础,包括过滤器逻辑、过滤器芯片、导出和实际网格。 All that works great so far.到目前为止,所有这些都很好。 Now i have instances where user can click on a row and edit some data which is displayed in a dialog window via angular material.现在我有一些实例,用户可以单击一行并编辑一些数据,这些数据通过 angular 材料显示在对话框 window 中。 ll that is working fine but what i am trying to do is to go and reload the grid after a user made changes or added data. ll 工作正常,但我想做的是 go 并在用户进行更改或添加数据后重新加载网格。 My Base component has a function called onGridRelaod我的 Base 组件有一个名为 onGridRelaod 的 function

 onGridReload($event: IGridReloadPayload) {
        this.setDataFetcherFactory($event.currentDataFetcherParams);
        this.agGridBase.setDataSource(() => {
        });
    }

So the question is how can i call the onGridRelaod from my dialog?所以问题是我如何从我的对话框中调用 onGridRelaod? I would assume i would have to expose it to the component which uses the base component and then have the dialog call it on the component which opened it.我假设我必须将它暴露给使用基本组件的组件,然后让对话框在打开它的组件上调用它。 But not 100 % sure how to go about it但不是 100% 确定如何 go 关于它

Try to emit the event from child component to parent.尝试将事件从子组件发送到父组件。

Dialog component:对话框组件:

...
onAdd = new EventEmitter();

onButtonClick() {
  this.onAdd.emit();
}
...

Parent Component:父组件:

...
let dialogRef = this.dialog.open(Component);
const sub = dialogRef.componentInstance.onAdd.subscribe(() => {
  // do something
});
dialogRef.afterClosed().subscribe(() => {
  // unsubscribe onAdd
});
...

Depending of your page structure but you can create sibling service that provide a signal Observable.根据您的页面结构,但您可以创建提供信号 Observable 的同级服务。 Your modal push inside this observable to notify "something change".您的模态推入此可观察对象以通知“某些变化”。 And your component can subscribe to it for reloading grid.您的组件可以订阅它以重新加载网格。

You can pass information like rowId for partial reload to speed up your reload.您可以传递诸如 rowId 之类的信息以进行部分重新加载以加快重新加载速度。

the service服务

@Injectable()
export class MyTableService {
  public change: EventEmitter<void> = new EventEmitter();
}

your modal你的模态

...
public onClose() {
  this.myTableService.change.next();
}
...

your component你的组件

...
public ngOnInit() {
  this.myTableService.change.subscribe(() => {
    this.onGridReload(...)
  })
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM