Home > Mobile >  How to set a asynchronously a variable in an angular pipe?
How to set a asynchronously a variable in an angular pipe?

Time:10-25

I'm new in angular, and i want to add a loading state to a button, but i don't want to make any service for it. I got really confused by pipes, and i dont't understant the right syntax. I can i set isLoading to false, when this.apiService.exportReport finished?

  public exportReportResults(format: ExportFormat) {
this.isLoading = true;
this.sink.add(
  this.service.context.pipe(
    filter((value) => !!value))
    .subscribe(
      (data) => {
    const items: any[] = [];
    data.context.groups.forEach(function (group) {
      items.push(group.items.map(({ id }) => id));
    });
    const itemQuery: string = "'{"   items.toString()   "}'";
    this.apiService.exportReport(format, itemQuery);
    this.isLoading = false; //not good this way of course
  },
  )
);

}

CodePudding user response:

Subscribe takes 3 arguments: Value consumer, exception handler, finalizer.

You can use finalizer to set required state after observable is closed.

If your observable is not closing at all, you have to do it in the consumer (as you did) or with tap operator somewhere in the pipe

for example

  this.service.context.pipe(
    filter((value) => !!value)),
    tap(v=>this.isLoading=false)).subscribe(...);
  • Related