Angular loading button ExpressionChangedAfterItHasBeenCheckedError
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
add a comment |
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
add a comment |
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
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
angular
asked Nov 16 '18 at 10:53
r3plicar3plica
4,4011448133
4,4011448133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
);
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
);
}
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
add a comment |
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)
);
}
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
add a comment |
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)
);
}
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)
);
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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