I'm using Angular 13 and ngrx/data, store and entity (v.13). I have set up my ngrx/data service
export class MyObjService extends DefaultDataService<MyObject> {
...
}
and then a component using PrimeNg. I have a table to display all my objects ...
<p-table #dt [value]="(myObjects$ | async)!" ...
In which the service file contains
constructor(
...
private service: MyObjService,
) {
...
this.myObjects$ = this.service.getAll();
The issue is every time I do an operation that alters the backend store, for example a delete
del(id: number){
this.myObjService.delete(id)
.subscribe(_ => {
this.MyObjects$ = this.myObjService.getAll();
} );
I have to refresh the table (I have to call "this.myObjects$ = this.myObjService.getAll();" above). Is there a way I can set the table so that the data in the table refreshes automatically? I feel like this is something ngrx/data would allow me to do but not sure how it is done.
First you can add a method to your service for listening an event: let's assume you have an
objectwhich contains your table data. let's sayCategoryis an object withid,nameand all data fields. Then you canimportSubject fromrxjswhich is an observable. in your service.ts file :then you can add a method as middleware:
then go to your
category.tsfile : import Subscription fromrxjsand store it in a variable.then the constructor :
in
ngOnInitfirst we get theCategoryfrom the service file. Like you are getting the datas. Then we execute our observation insubscription.And we set the object value through it.then you don't need to use other observable anymore it will handle everything.