Home > Enterprise >  Modify data saved in shared data service before returning it
Modify data saved in shared data service before returning it

Time:01-07

I have a shared service as below :

private data= new BehaviorSubject<any>(null);

setData(obj) {
    this.data.next(obj);
}

getData(filter): Observable<any> {
//need to filter data based on the parameter before returning
    return this.data.asObservable();
}

How can we modify an observable before returning the value?

Env: Angular 14

CodePudding user response:

You can add a pipe(filter(data => [your condition])) to the observable you return:

  getData(filter): Observable<any> {
    return this.data.asObservable()
     .pipe(filter(data => ...));
  }
  • Related