简体   繁体   English

检查后表达式已更改-角度

[英]Expression has changed after it was checked - angular

When I call a service in (change) action method , I got this error : 当我在(change)action方法中调用服务时,出现此错误:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. 错误错误:ExpressionChangedAfterItHasBeenCheckedError:检查表达式后,表达式已更改。 Previous value: 'ng-untouched: true'. 先前的值:“ ng-untouched:true”。 Current value: 'ng-untouched: false' 当前值:“ ng-untouched:false”

My code : 我的代码:

onChange(event) { 

  const messageToDisplay = "Message test";
  this.popinService.open(messageToDisplay).subscribe((result: ConfirmPopinResultType) => {
    if (result === ConfirmPopinResultType.CONFIRM) {
      console.log("test");
    }
  });        
}

onChange is an event handler on an input reactive form (select dropdown list) onChange是输入反应形式(选择下拉列表)上的事件处理程序

<form [formGroup]="poolForm"> 
  <select id="pool-select" #item (change)="item.value = onChangePool(item.value)" formControlName="item"> 
     <option *ngFor="let item of items" [value]="item.name">{{ item.name }}</option> 
  </select> 
</form> 

The exception error occurs when I call the poppin service on Change event handler (that display a pop up with two button Confirm and Cancel) 当我在Change事件处理程序上调用poppin服务时发生异常错误(该事件显示带有两个按钮Confirm和Cancel的弹出窗口)

thank for your help! 谢谢您帮忙!

You are listening to DOM element events. 您正在听DOM元素事件。

(change)="onChange(item.value)"

And you are receiving an error relative to Angular forms. 并且您收到相对于Angular表单的错误。

Previous value: 'ng-untouched: true'. 先前的值:“ ng-untouched:true”。 Current value: 'ng-untouched: false' 当前值:“ ng-untouched:false”

The two are in conflict 两者有冲突
because the ngModel is tracking changes to the form component. 因为ngModel跟踪表单组件的更改。
Angular Forms use (ngModelChange) to notify of component changes. Angular Forms使用(ngModelChange)通知组件更改。
Angular Reactive Forms use valueChanges observables. 角反应形式使用valueChanges可观察对象。

You haven't shared where you've attached the (change) event handler, but it's clearly on a form input that is also being used by ngModel . 您尚未共享附加(change)事件处理程序的位置,但是显然在ngModel还在使用的表单输入上。

You should be using (ngModelChange) instead of (change) . 您应该使用(ngModelChange)而不是(change)

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

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