ERROR Error: ExpressionChangedAfterItHasBeenCheckedError in Angular 5

夙愿已清 提交于 2019-12-11 16:54:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!