Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression has changed after it was checked - angular

When I call a service in (change) action method , I got this error :

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Current value: '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)

<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)

thank for your help!

like image 385
Abadou Avatar asked Jan 02 '19 21:01

Abadou


People also ask

How do you resolve expression has changed after it was checked?

You basically have two alternatives how to fix it: update the property asynchronously either using setTimeout , Promise. then or asynchronous observable referenced in the template. perform the property update in a hook before the DOM update - ngOnInit, ngDoCheck, ngAfterContentInit, ngAfterContentChecked.

How do you fix ng0100 expression has changed after it was checked?

Navigate up the call stack until you find a template expression where the value displayed in the error has changed. Ensure that there are no changes to the bindings in the template after change detection is run. This often means refactoring to use the correct component lifecycle hook for your use case.

What is the difference between ngAfterContentInit and ngAfterViewInit?

ngAfterContentInit : This is called after components external content has been initialized. ngAfterViewInit : This is called after the component view and its child views has been initialized.

What is ngAfterContentInit?

ngAfterContentInit()linkA callback method that is invoked immediately after Angular has completed initialization of all of the directive's content. It is invoked only once when the directive is instantiated.


1 Answers

You are listening to DOM element events.

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

And you are receiving an error relative to Angular forms.

Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false'

The two are in conflict
because the ngModel is tracking changes to the form component.
Angular Forms use (ngModelChange) to notify of component changes.
Angular Reactive Forms use valueChanges observables.

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.

You should be using (ngModelChange) instead of (change).

like image 95
Reactgular Avatar answered Oct 06 '22 01:10

Reactgular