简体   繁体   English

角度抛出ExpressionChangedAfterItHaHasBeenCheckedError

[英]Angular throws ExpressionChangedAfterItHasBeenCheckedError

I am working on a angular application with angular 7.0.4. 我正在使用角度为7.0.4的角度应用程序。

I want to set the focus on the first input element of the modal if my workingtimes list has more elements than 1 in it. 如果我的工作时间列表中的元素多于1,我想将焦点放在模式的第一个输入元素上。 The problem is if I open the modal it throws me this exception: 问题是,如果我打开模态,则会抛出此异常:

ModalComponent.html:23 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false'.
    at viewDebugError (core.js:16878)
    at expressionChangedAfterItHasBeenCheckedError (core.js:16866)
    at checkBindingNoChanges (core.js:16968)
    at checkNoChangesNodeInline (core.js:19839)
    at checkNoChangesNode (core.js:19828)
    at debugCheckNoChangesNode (core.js:20432)
    at debugCheckRenderNodeFn (core.js:20386)
    at Object.eval [as updateRenderer] (ModalComponent.html:23)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:20375)
    at checkNoChangesView (core.js:19729)

The error occurs on this lines of code in the modal.component.html file: 在modal.component.html文件中的以下代码行上发生错误:

<input class="input" type="time" [(ngModel)]="time.From" [autofocus]="workingDay.Times?.length > 1">

The [autofocus] directive looks like this: [autofocus]指令如下所示:

export class AutoFocusDirective implements OnInit{
  @Input('autofocus') enabled: boolean = true;
  constructor(
    private element: ElementRef
  ) { 
  }

  ngOnInit() {
    if (this.enabled) {
      (this.element.nativeElement as HTMLInputElement).focus();
    }
  }
}

I actually can not see the reason why this occures. 我实际上看不到发生这种情况的原因。

Thank you for your help on advance. 谢谢您的帮助。

Try adding (this.element.nativeElement as HTMLInputElement).focus(); 尝试添加(this.element.nativeElement as HTMLInputElement).focus(); this into setTimeOut 这进入setTimeOut

Sometimes binding will not occur as per what we think, and value changes are there in binding when during the digest cycle so in most of that cases this error occurred. 有时,绑定不会按照我们的想法发生,并且在摘要周期内绑定中会发生值更改,因此在大多数情况下会发生此错误。

For more details refer - 有关更多详细信息,请参考-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度中的 getter 值抛出 ExpressionChangedAfterItHasBeenCheckedError - getter value in angular throws ExpressionChangedAfterItHasBeenCheckedError Angular 加载错误 ExpressionChangedAfterItHasBeenCheckedError - Angular Loading Error ExpressionChangedAfterItHasBeenCheckedError 错误错误:Angular 5中的ExpressionChangedAfterItHasBeenCheckedError - ERROR Error: ExpressionChangedAfterItHasBeenCheckedError in Angular 5 Angular 4应用程序中的ExpressionChangedAfterItHasBeenCheckedError错误 - ExpressionChangedAfterItHasBeenCheckedError Error in Angular 4 Application Angular:尝试禁用按钮时出现 ExpressionChangedAfterItHasBeenCheckedError - Angular: ExpressionChangedAfterItHasBeenCheckedError when trying to disable button Angular 8 在页面加载后添加 class 错误:ExpressionChangedAfterItHasBeenCheckedError - Angular 8 add class after page load Error: ExpressionChangedAfterItHasBeenCheckedError 在进度条中使用随机数时出现 Angular ExpressionChangedAfterItHasBeenCheckedError - Angular ExpressionChangedAfterItHasBeenCheckedError while using random numbers in progress bar 错误:ExpressionChangedAfterItHasBeenCheckedError - ERROR : ExpressionChangedAfterItHasBeenCheckedError Angular 4重置按钮引发错误 - Angular 4 Reset Button Throws Error Angular JS:$ scope中的HTML引发jshint错误? - Angular JS: HTML in $scope throws jshint error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM