Angular loading button ExpressionChangedAfterItHasBeenCheckedError












0















I have this component I use in my application and for the most part it works fine.
It's ts file looks like this:



import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { Router } from '@angular/router';

import { LoadingService } from '@pyb-services/loading.service';

@Component({
selector: 'pyb-loading-button',
templateUrl: './loading-button.component.html',
styleUrls: ['./loading-button.component.scss']
})
export class LoadingButtonComponent implements OnInit, OnChanges {
@Input() link: any =
@Input() target: string
@Input() navigateAfter: boolean
clicked: boolean = false
loading: boolean = false; // Has to be false by default, because sometimes everything has loaded before this button has come on the page

constructor(
private router: Router,
private loadingService: LoadingService
) {
}

ngOnInit() {
this.loadingService.onLoadingChanged.subscribe(isLoading => this.loading = isLoading);
}

ngOnChanges() {
this.navigateWhen();
}

click(): void {
this.clicked = !this.clicked;
this.navigateWhen();
}

private navigateWhen(): void {
if ((!this.loading || this.navigateAfter) && this.clicked && this.link.length) {
this.router.navigate(this.link);

this.clicked = false;
}
}
}


And the template file looks like this:



<button class="btn btn-xl btn-secondary" type="button" [disabled]="loading && !navigateAfter" (click)="click()" [scrollTo]="target"
offset="10" [ngSwitch]="loading">
<span class="spinner" *ngSwitchCase="true">
<span class="bounce1"></span>
<span class="bounce2"></span>
<span class="bounce3"></span>
</span>
<span *ngSwitchDefault>Next step</span>
</button>


Like I have said, it seems to work fine.
But, if another process kicks off an http request, then I get the error:




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




which is moaning about the loading property changing in the loading button component.
Does anyone know how I can fix this?










share|improve this question



























    0















    I have this component I use in my application and for the most part it works fine.
    It's ts file looks like this:



    import { Component, Input, OnInit, OnChanges } from '@angular/core';
    import { Router } from '@angular/router';

    import { LoadingService } from '@pyb-services/loading.service';

    @Component({
    selector: 'pyb-loading-button',
    templateUrl: './loading-button.component.html',
    styleUrls: ['./loading-button.component.scss']
    })
    export class LoadingButtonComponent implements OnInit, OnChanges {
    @Input() link: any =
    @Input() target: string
    @Input() navigateAfter: boolean
    clicked: boolean = false
    loading: boolean = false; // Has to be false by default, because sometimes everything has loaded before this button has come on the page

    constructor(
    private router: Router,
    private loadingService: LoadingService
    ) {
    }

    ngOnInit() {
    this.loadingService.onLoadingChanged.subscribe(isLoading => this.loading = isLoading);
    }

    ngOnChanges() {
    this.navigateWhen();
    }

    click(): void {
    this.clicked = !this.clicked;
    this.navigateWhen();
    }

    private navigateWhen(): void {
    if ((!this.loading || this.navigateAfter) && this.clicked && this.link.length) {
    this.router.navigate(this.link);

    this.clicked = false;
    }
    }
    }


    And the template file looks like this:



    <button class="btn btn-xl btn-secondary" type="button" [disabled]="loading && !navigateAfter" (click)="click()" [scrollTo]="target"
    offset="10" [ngSwitch]="loading">
    <span class="spinner" *ngSwitchCase="true">
    <span class="bounce1"></span>
    <span class="bounce2"></span>
    <span class="bounce3"></span>
    </span>
    <span *ngSwitchDefault>Next step</span>
    </button>


    Like I have said, it seems to work fine.
    But, if another process kicks off an http request, then I get the error:




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




    which is moaning about the loading property changing in the loading button component.
    Does anyone know how I can fix this?










    share|improve this question

























      0












      0








      0








      I have this component I use in my application and for the most part it works fine.
      It's ts file looks like this:



      import { Component, Input, OnInit, OnChanges } from '@angular/core';
      import { Router } from '@angular/router';

      import { LoadingService } from '@pyb-services/loading.service';

      @Component({
      selector: 'pyb-loading-button',
      templateUrl: './loading-button.component.html',
      styleUrls: ['./loading-button.component.scss']
      })
      export class LoadingButtonComponent implements OnInit, OnChanges {
      @Input() link: any =
      @Input() target: string
      @Input() navigateAfter: boolean
      clicked: boolean = false
      loading: boolean = false; // Has to be false by default, because sometimes everything has loaded before this button has come on the page

      constructor(
      private router: Router,
      private loadingService: LoadingService
      ) {
      }

      ngOnInit() {
      this.loadingService.onLoadingChanged.subscribe(isLoading => this.loading = isLoading);
      }

      ngOnChanges() {
      this.navigateWhen();
      }

      click(): void {
      this.clicked = !this.clicked;
      this.navigateWhen();
      }

      private navigateWhen(): void {
      if ((!this.loading || this.navigateAfter) && this.clicked && this.link.length) {
      this.router.navigate(this.link);

      this.clicked = false;
      }
      }
      }


      And the template file looks like this:



      <button class="btn btn-xl btn-secondary" type="button" [disabled]="loading && !navigateAfter" (click)="click()" [scrollTo]="target"
      offset="10" [ngSwitch]="loading">
      <span class="spinner" *ngSwitchCase="true">
      <span class="bounce1"></span>
      <span class="bounce2"></span>
      <span class="bounce3"></span>
      </span>
      <span *ngSwitchDefault>Next step</span>
      </button>


      Like I have said, it seems to work fine.
      But, if another process kicks off an http request, then I get the error:




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




      which is moaning about the loading property changing in the loading button component.
      Does anyone know how I can fix this?










      share|improve this question














      I have this component I use in my application and for the most part it works fine.
      It's ts file looks like this:



      import { Component, Input, OnInit, OnChanges } from '@angular/core';
      import { Router } from '@angular/router';

      import { LoadingService } from '@pyb-services/loading.service';

      @Component({
      selector: 'pyb-loading-button',
      templateUrl: './loading-button.component.html',
      styleUrls: ['./loading-button.component.scss']
      })
      export class LoadingButtonComponent implements OnInit, OnChanges {
      @Input() link: any =
      @Input() target: string
      @Input() navigateAfter: boolean
      clicked: boolean = false
      loading: boolean = false; // Has to be false by default, because sometimes everything has loaded before this button has come on the page

      constructor(
      private router: Router,
      private loadingService: LoadingService
      ) {
      }

      ngOnInit() {
      this.loadingService.onLoadingChanged.subscribe(isLoading => this.loading = isLoading);
      }

      ngOnChanges() {
      this.navigateWhen();
      }

      click(): void {
      this.clicked = !this.clicked;
      this.navigateWhen();
      }

      private navigateWhen(): void {
      if ((!this.loading || this.navigateAfter) && this.clicked && this.link.length) {
      this.router.navigate(this.link);

      this.clicked = false;
      }
      }
      }


      And the template file looks like this:



      <button class="btn btn-xl btn-secondary" type="button" [disabled]="loading && !navigateAfter" (click)="click()" [scrollTo]="target"
      offset="10" [ngSwitch]="loading">
      <span class="spinner" *ngSwitchCase="true">
      <span class="bounce1"></span>
      <span class="bounce2"></span>
      <span class="bounce3"></span>
      </span>
      <span *ngSwitchDefault>Next step</span>
      </button>


      Like I have said, it seems to work fine.
      But, if another process kicks off an http request, then I get the error:




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




      which is moaning about the loading property changing in the loading button component.
      Does anyone know how I can fix this?







      angular






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 10:53









      r3plicar3plica

      4,4011448133




      4,4011448133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Here is the issue with variable loading which is being changed while Change Detection has been trigger. To avoid this error set the value of loading in separate thread.



            ngOnInit() {
          this.loadingService.onLoadingChanged.subscribe(isLoading =>
          Promise.resolve(null).then(() => this.loading = isLoading)
          );
          }





          share|improve this answer
























          • might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

            – Carsten
            Nov 16 '18 at 11:13











          • That is also doable but not recommendable.

            – Sunil Singh
            Nov 16 '18 at 11:14












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336402%2fangular-loading-button-expressionchangedafterithasbeencheckederror%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Here is the issue with variable loading which is being changed while Change Detection has been trigger. To avoid this error set the value of loading in separate thread.



            ngOnInit() {
          this.loadingService.onLoadingChanged.subscribe(isLoading =>
          Promise.resolve(null).then(() => this.loading = isLoading)
          );
          }





          share|improve this answer
























          • might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

            – Carsten
            Nov 16 '18 at 11:13











          • That is also doable but not recommendable.

            – Sunil Singh
            Nov 16 '18 at 11:14
















          0














          Here is the issue with variable loading which is being changed while Change Detection has been trigger. To avoid this error set the value of loading in separate thread.



            ngOnInit() {
          this.loadingService.onLoadingChanged.subscribe(isLoading =>
          Promise.resolve(null).then(() => this.loading = isLoading)
          );
          }





          share|improve this answer
























          • might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

            – Carsten
            Nov 16 '18 at 11:13











          • That is also doable but not recommendable.

            – Sunil Singh
            Nov 16 '18 at 11:14














          0












          0








          0







          Here is the issue with variable loading which is being changed while Change Detection has been trigger. To avoid this error set the value of loading in separate thread.



            ngOnInit() {
          this.loadingService.onLoadingChanged.subscribe(isLoading =>
          Promise.resolve(null).then(() => this.loading = isLoading)
          );
          }





          share|improve this answer













          Here is the issue with variable loading which is being changed while Change Detection has been trigger. To avoid this error set the value of loading in separate thread.



            ngOnInit() {
          this.loadingService.onLoadingChanged.subscribe(isLoading =>
          Promise.resolve(null).then(() => this.loading = isLoading)
          );
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 11:08









          Sunil SinghSunil Singh

          6,4472628




          6,4472628













          • might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

            – Carsten
            Nov 16 '18 at 11:13











          • That is also doable but not recommendable.

            – Sunil Singh
            Nov 16 '18 at 11:14



















          • might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

            – Carsten
            Nov 16 '18 at 11:13











          • That is also doable but not recommendable.

            – Sunil Singh
            Nov 16 '18 at 11:14

















          might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

          – Carsten
          Nov 16 '18 at 11:13





          might as wel surround it with a simple setTimeout if you don't want to solve the issue itself..

          – Carsten
          Nov 16 '18 at 11:13













          That is also doable but not recommendable.

          – Sunil Singh
          Nov 16 '18 at 11:14





          That is also doable but not recommendable.

          – Sunil Singh
          Nov 16 '18 at 11:14




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336402%2fangular-loading-button-expressionchangedafterithasbeencheckederror%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine