Encountering the "ExpressionChangedAfterItHasBeenCheckedError" in Angular 2

As I try to fill in multiple rows within a table that I've created, the table gets populated successfully. However, an error message pops up:

"ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '1'. Current value: 'ah'."

Although this error doesn't crash my program, whenever I modify any unit in the table, all units end up taking on the same value as indicated by the "Current value" in the error above (in this case, it shows 'ah,' view code below)

This is how I'm adding data to the table:

<div class="row-container">
  <div *ngFor="let week of getWeeks()" class="row">
    <input class="week" maxlength="2" (input)="type($event.target.value, week)" value="{{getCapacity(week)}}">
  </div>
</div>


And here's the methodology I employ to update the values:

getCapacity(week: Date): string {
    if (<conditions are met>) { 
        const capacity = EntryComponent.capacity[EntryComponent.index];
        EntryComponent.index++;
        return capacity.value;
    }
    return 'ah';
}

I am not using a dynamic component as a tag, instead I am dynamically altering the value of my input tag.

Answer №1

When Angular is in development mode, it performs two change detection loops - one as usual and a second to verify that no changes occurred during the initial loop. If any changes are detected, the ExpressionChangedAfterItHasBeenCheckedError will be triggered. This does not mean your application won't function properly; rather, Angular is simply alerting you of this behavior.

In this specific scenario, the error lies within this piece of code:

if (<conditions are met>) {  <--- Creating behavior that causes error
    const capacity = EntryComponent.capacity[EntryComponent.index];
    EntryComponent.index++;
    return capacity.value;
}
return 'ah';

If anything inside the change loop alters that condition, the first loop will return capacity.value, causing the condition to change. As a result, the second loop will not meet the condition and instead return ah. This cycle leads to the error, serving as a valid warning: I advise against basing a binding target on a conditional that may change during a change loop. I suggest revising this condition to depend on a component property instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem: When trying to access the property 'completed' of an object in Angular 2, an error is

My understanding is that by using @Input(), this component should be able to bind <experiment [experiment]="experiment.completed"></experiment>. Here is the hierarchy: https://i.stack.imgur.com/6UwHt.png experiment.detail.component.ts import ...

Removing a specific MySQL row using HTML in collaboration with Node.js

I've been working on a feature to allow users to delete specific rows from a table. Each row has a "Delete" link at the end, but when I click it, nothing happens. There are no errors displayed, and the console shows that 0 row(s) have been updated, ye ...

The error message "Property 'map' is not found on type 'User' in React with typescript"

I am currently experimenting with React using TypeScript. I have set up useState with an object but I am encountering issues trying to use the map function with that object. Below is the error message I am facing: Property 'map' does not exist ...

Tips for storing data from a table using a button

I have implemented a data table to display information from a database. I am looking for a way to save user information whenever they export data in Excel, CSV, or PDF format from the table. I have already created a function to handle saving user data, but ...

Are the functions 'useEffect' and 'useCallback' being repetitively used in this case?

I have implemented a custom hook in my React application to manage back navigation actions (POP). The functionality is operational, but I can't help but notice that useCallback and useEffect seem to be performing similar tasks. Here is the snippet of ...

Using the goBack function in React Router does not add the previous location to the stack

In React Router v4, I have a list page and a details page in my web application. I want to implement a 'Save and close' button on the details page that redirects the user back to the list page when clicked. However, I noticed that after the user ...

Exploring the functionality of $scope.$watch in ES6

I'm encountering a problem while utilizing $scope.$watch in my ES6 project. The watch triggers once and then doesn't work thereafter. Below is the snippet of code: export class SomeController { constructor($log, $scope) { 'ngInject&a ...

What is the best way to modify the KeyName in an object?

Having difficulty parsing an Object by changing keynames due to the error message "Element implicitly has an 'any' type because expression of type 'keyof SignInStore' can't be used to index type '{}'". interface SignInSto ...

`We enhance collaboration within sibling next components by exchanging information`

Completely new to web development, I have been working on an app with a navbar that allows users to select items from a drop-down menu. My specific issue is trying to access the title.id in a sibling component, but it keeps coming up as null. PARENT COMPO ...

Unable to capture HTML form input in $_POST[]

I've encountered an unusual issue while transferring data from an email form (HTML5) to ajax/JSON and receiving false in order to prevent redirection to the php script after pressing the submit button. When I include commas between each data paramete ...

Looking for a sublime plugin that will enhance your angular view template?

When using Sublime Text with Angular (2/4), everything works great until I'm working in the view template file. The interpolation doesn't offer auto-completion for class properties that support the view, unlike what I've seen in VS Code. Ar ...

Generating swagger documentation for TypeScript-based Express applications

I have successfully set up the swagger URL following a helpful guide on configuring Swagger using Express API with autogenerated OpenAPI documentation through Swagger. Currently, I am utilizing TypeScript which outputs .js files in the dist folder without ...

What is the best way to save emitted values from rxjs?

I'm currently grappling with understanding rxjs. In my angular project, I've created a service that emits and listens to boolean values that indicate the start and completion of asynchronous operations. export class UpdateScriptRunningEventServi ...

Can someone guide me on incorporating bluebird promises with request-extensible?

I'm interested in utilizing the bluebird library to create a promise-based asynchronous web client. Currently, I have been using the request-promise package for this purpose. To get started, I simply include the following lines of code at the beginnin ...

The coordinates of the event do not match the coordinates of the location. Successful AJAX response data

How can I retrieve the accurate latitude and longitude when the Google Maps marker finishes dragging? It's confusing because for the same exact point, two different (but very approximate) coordinates are provided. Here are some example results for t ...

DirectionsLight isn't functioning properly in Three.js

I attempted to add lighting in Three.js, but after updating the display, the light didn't seem to affect my cylinder. I simply copied and pasted the source code from the Three.js documentation Three.js as instructed, but to no avail. Below is the cod ...

Move the navigation bullets of the Nivo Slider to the bottom instead of below the slider

Currently working on a website and have incorporated Nivo Slider to develop a jQuery slider. Encountering an issue with the bullets that indicate which slide the user is currently viewing. I'd like these bullets to be displayed on the images within t ...

Streamlined method for creating forms and charts within SharePoint

When it comes to creating charts and forms on SharePoint, what is the best approach for maximizing efficiency? Using AngularJS web parts with CRUD operations. Modifying standard forms and integrating charts using JavaScript libraries and CSS. Are there ...

Unable to show the response from an HTML servlet using Ajax and qTip2

I am having an issue where I am trying to display text (or html) received from a servlet response in a qTip2 tooltip within a jsp page. Despite verifying that the servlet is being invoked and returning text using Firebug, I encountered an error when attemp ...

Connect your Angular2 app to the global node modules folder using this link

Is there a way to set up a centralized Node modules folder on the C disk instead of having it locally within the app directory? This would be more convenient as Angular2 CLI tends to install over 125mb of Node modules in the local folder. In our TypeScrip ...