问题
I am new to angular 5 ,Here I am trying to disable a input field based on the condition.
<div *ngIf="isOTPFieldEnabled" class="email-field-width">
<mat-form-field class="email-field-width">
<mat-label> Enter OTP</mat-label>
<input [attr.disabled]="isDiableSignInOTPField" #OTP (click)="getOTP(OTP.value)" [formControl]="signInOTP" maxlength="6" matInput required placeholder="OTP">
<mat-hint [ngStyle]="{color: hintColor}">{{hintOTP}}</mat-hint>
</mat-form-field>
</div>
After the page load this div is not visible to the user .Once the *ngIf="isOTPFieldEnabled"
value is meets TRUE it will be visible to the user.
And once the user input validated in the getOTP() method I want to disable this input field .
For that I have set [attr.disabled]
the functionality is working fine but I got the error as I mentioned in my title.
Detailed Error :
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'mat-focused: true'. Current value: 'mat-focused: false'.
at viewDebugError (core.js:7290)
at expressionChangedAfterItHasBeenCheckedError (core.js:7278)
at checkBindingNoChanges (core.js:7380)
at checkNoChangesNodeDynamic (core.js:10263)
at checkNoChangesNode (core.js:10233)
at debugCheckNoChangesNode (core.js:10833)
at debugCheckRenderNodeFn (core.js:10787)
at Object.eval [as updateRenderer] (CheckoutComponent.html:159)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:10776)
at checkNoChangesView (core.js:10131)
UPDATE:
export class CheckoutComponent implements OnInit {
isOTPFieldEnabled:boolean=false;
isDiableSignInOTPField:boolead =false;
ngOnInit(){}
emailVerified(){
this.isOTPFieldEnabled=true;
}
getOTP(OTP){
if(OTP){
this.isDiableSignInOTPField=true;
}
}
}
回答1:
Looking at the lifecycle hooks documentation
Change ngOnInit()
to ngAfterContentInit()
.
It might help you.
ngAfterContentInit(){
this.emailVerified();
this.getOTP(OTP);
}
this.emailVerified() {
this.isOTPFieldEnabled=true;
}
this.getOTP(OTP) {
if(OTP) {
this.isDiableSignInOTPField=true;
}
}
来源:https://stackoverflow.com/questions/52272192/error-error-expressionchangedafterithasbeencheckederror-in-angular-5