0

I have mi in place an http request spinner that appears if a request is in progress. Except that I get the following error :

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.

<div class="container-fluid">
  <div class="row col-md-12">
    <div class="spinner-style" *ngIf="isLoading | async">
      <mat-spinner spinner-delay="2000"></mat-spinner>
    </div>

    <app-another-comp-table[isLoading]="isLoading | async">
.....
    </app-another-comp-table>
  </div>
</div>

My method init in my component :

ngOnInit(){
    this.isLoading = this.loaderService.getIsLoading$();

...
}

My Loader service :

export class LoaderService {
  public isLoading = new BehaviorSubject(false);

  constructor() { }

  getIsLoading$(): Observable<boolean> {
    return this.isLoading.asObservable();
  }
  
}
4
  • You have to show more code where you are using isLoading. My guess is that you are executing a code in AfterViewInit ... Commented Feb 26, 2021 at 23:13
  • I’ve updated my question. I subscribe to loading changes in ngOnInit. This is an observable Commented Feb 26, 2021 at 23:20
  • Can you please show the code of getIsLoading$() ? Commented Feb 26, 2021 at 23:36
  • I add my LoadService Commented Feb 26, 2021 at 23:38

1 Answer 1

1

You get this error because isLoading changes from false to true. When you declare isLoading, his default value will be false. The DOM did not have time to process the false value, and it changes to true when your observable end.

RxJS is strictly sequential and subscriptions happen synchronously.

A solution can be to wrap your variable with setTimeout() or with NgZone.run().

You can also do this with rxjs using subscribeOn(async) operator.

import { asyncScheduler } from 'rxjs';
import { observeOn } from 'rxjs/operators';
...
getIsLoading$(): Observable<boolean> {
  return this.isLoading.asObservable()
  .pipe(observeOn(asyncScheduler);
}

PS : You should end the name of an observable with $ (isLoading$)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.