#Angular loading state for API request

9 messages · Page 1 of 1 (latest)

slate birch
#

I am trying to do something simple and I keep getting a
NG0100: Expression has changed after it was checked

I have a component that defines a class variable loading and sets it to false

then ngOnInit (tried after viewInit as well)

I set it to loading to true
await the api call
set loading to false

I use this in my template to show wait on showing data until it is loaded

I know why the error is happening, and I can fix with using ChangeDetectionRef, but I have read that I should generally avoid that.

Is there a good solution to this?

thorn nimbus
#

Hello! I had the same problem and I solve it using:

#
  ngOnInit(): void {
    setTimeout(() => {
      this.getProfiles();
    });
  }
slate birch
#

Yeah I found that to work as well, seems so hacky. There has to be a more elegant solution to this common pattern.

lusty pilot
#

use a Signals with change detection in mode on push ?

slate birch
#

I tried signals, still the same error, unless I was using them wrong. I want to stay away from manually using change detection

#

@lusty pilot is there a way to use signals without manually using change detection?

lusty pilot
#

Yes with use of Change detective OnPush