简体   繁体   中英

Angular Rxjs: emit all merged observables with delay continuously with async pipe

Hello guys I'm trying to use a data object in html file and i'm using the async pipe and a subject to emit id and get server response.
Here is my code:

     logDetails$: Observable<LogDetails>;
     getDetails$ = new Subject<string>();

     this.logDetails$ = this.getDetails$.pipe(
        map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id", id)),
        switchMap(apiRoute => this.http.get<LogDetails>(apiRoute))
    );

I use an async pipe in my view to use subscribe for result.

*ngIf="logDetails$ | async; let details"

Now i want this behaviour: I emit the getDetails$ with id from multiple locations.
Then i need that before server call a null value for result get emmited to the view and then the server response (LogDetails object) after some delay.

  1. send a default value for result
  2. delay
  3. send server response

Can i use operators to achieve this?

You can use startWith and delay .

this.logDetails$ = this.getDetails$.pipe(
  map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id", id)),
  switchMap(apiRoute => this.http.get<LogDetails>(apiRoute).pipe(
    delay(1000),
    startWith(null)
  )),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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