When I try to use [ngClass] in my Angular application, I get the following error:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngClass: undefined'.
It works when I call a change after I got the error, the issue seems to be during initialization.
Here is the HTML:
<div class="switch-wrapper">
<button [ngClass]="loginSelected ? 'selected' : ''" (click)="selectLogin()">
Login
</button>
<button [ngClass]="signupSelected ? 'selected' : ''" (click)="selectSignup()">
Signup
</button>
</div>
And here is the TS:
export class LoginComponent implements AfterContentInit {
loginSelected = true;
signupSelected = false;
constructor(private cdRef: ChangeDetectorRef) {
}
ngAfterContentInit() {
this.cdRef.detectChanges();
}
selectLogin() {
this.loginSelected = true;
this.signupSelected = false;
}
selectSignup() {
this.loginSelected = false;
this.signupSelected = true;
}
}
This questions has been asked many times on here, but the suggested solutions don't seem to work for me. Things I've Tried:
- Not using the ChangeDetector.
- Doing changes and then calling .detechChanges().
- Calling .detechChanges() and then doing changes.
- Just initializing the values.
- Using ngOnInit and the different types of ngAfter***().
- Any combinations of the above.
If someone could help me, I'd greatly appreciate it. I know the error is probably on my part, and this is driving me nuts. Thanks!