简体   繁体   English

Angular 6:从选择下拉事件触发的ExpressionChangedAfterItHasBeenCheckedError

[英]Angular 6: ExpressionChangedAfterItHasBeenCheckedError triggered from select drop-down event

What code changes can remedy the Error described below ? 哪些代码更改可以解决下面描述的错误?

Use-case 用例

A drop-down input UI is protected from unintended selected value change via a modal. 通过模式保护下拉输入UI免受意外的选定值更改。 However, the event (click, focus or other), wired to the drop-down causes the ExpressionChangedAfterItHasBeenCheckedError, observed in Chrome console in dev mode. 但是,连接到下拉列表的事件(单击,焦点或其他)会导致在开发模式下在Chrome控制台中观察到的ExpressionChangedAfterItHasBeenCheckedError。

Observed result 观察结果

When drop-down is clicked, an ExpressionChangedAfterItHasBeenCheckedError is thrown (see console) 单击下拉列表时,将抛出ExpressionChangedAfterItHasBeenCheckedError (请参阅控制台)

Expected result 预期结果

When drop-down is clicked, a Modal opens without error 单击下拉列表时,将打开模态而不会出现错误

Demo 演示

A link to demo in stackblitz stackblitz中的演示链接

Notes 笔记

  • As described in "Everything you need to know about ExpressionChangedAfterItHasBeenCheckedError" here , I attempt to trigger change detection (see comments inside app.component.ts:48-49, marked as STEP-1 and STEP-2), but unsuccessfully (probably triggered not in the right place?) 如“所有你需要知道ExpressionChangedAfterItHasBeenCheckedError” 在这里 ,我试图触发变化检测(见注释里面app.component.ts:48-49,标示为STEP-1和STEP-2),但没有成功(可能是触发不是在正确的地方?)
  • The modal in the demo is not fully implemented for [OK] and [CANCEL] as it doesn't affect the Error 演示中的模态未完全实现[OK]和[CANCEL],因为它不会影响错误
  • The code is simplified version of a larger app 该代码是较大的应用程序的简化版本

What code changes can remedy the Error described below ? 哪些代码更改可以解决下面描述的错误?

The code like this should help you: 像这样的代码可以帮助你:

<select 
  [(ngModel)]="building.venueId"
  #ngModel="ngModel"
  ^^^^^^^^^^^^^^^^^^
  get hold of NgModel instance
  ...
  (click)="ngModel.control.markAsTouched(); confirmChangeItem(building.venueId)">
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           and prepare FormControl to the changes

Forked Stackblitz 分叉Stackblitz

Update 更新

I forgot to say that you can remove cdRef.detectChanges and microtask in this case so i'm glad to know you guessed it yourself 我忘了说你可以在这种情况下删除cdRef.detectChanges和microtask所以我很高兴知道你自己猜对了

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM