The ng-model used within an *ngFor loop is not displaying the correct value

I am working with a JSON file in my Angular2 App.

The specific JSON object I am referring to is named tempPromotion and I am trying to access the data within ['results'] like this:

tempPromotion['response_payload']['ruleset_list']['results']

Within ['results'], there are two values, ['value_path'] and ['calculation']

This is how my HTML code looks:

  <table class="table table-striped">
      <thead>
        <tr>
          <th>Value Path</th>
          <th>Result</th>
          <th>TEST</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let result of rule['results']; let x = index">
          <td><input type="text" class="form-control" id="valuePath{{x}}" [(ngModel)]="result['value_path']" name="valuePath{{x}}"></td>
          <td><input type="text" class="form-control" id="discount{{x}}" [(ngModel)]="result['calculation']" name="discount{{x}}"></td>
          <td>{{x}} {{result['calculation']}}</td>
        </tr>
      </tbody>
    </table>

It's important to note that I am using rule because it is already inside another *ngfor loop here:

<div class="card" *ngFor="let rule of tempPromotion['response_payload']['ruleset_list']; let i = index">

When I look at the output, it appears like this:

channel 1 | #Value * 0.8 | 0 #Value * 0.9 
channel 1 | #Value * 0.8 | 1 #Value * 0.8 

Although my [(ngModel)] displays incorrect values, the Two-way binding {{result['calculation']}} shows the correct values.

If someone could explain why this discrepancy exists and offer a solution, I would greatly appreciate it.

Answer №1

Hooray! I was able to make it work.

The issue I encountered was using the same id / name for my inputs.

By changing from id="valuePath{{x}}" to id="valuePath{{i}}{{x}}", I was able to resolve the problem.

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

Tips for using a TypeScript method decorator while maintaining the expected `this` scope

It was brought to my attention that the issue I encountered was due to the use of GraphQL resolvers in running my decorated method. This resulted in the scope of this being undefined. Nevertheless, the core of the question provides valuable insights for an ...

Error: Angular variable is undefined when using template-driven validation

When I create a form using a template-driven approach and submit it, an error occurs stating that all form fields are undefined. The form consists of five fields, and I attempt to clear them using the reset form function. As a novice in Angular, I am uncer ...

Updating ES6 syntax for superset in array: a step-by-step guide

I am currently working with ES6 React code that generates an array of MiniIcons on a webpage. const MiniIcons = ({miniicons}) => ( <div id="application"> {miniicons.map(miniicon => ( <MiniIcon key={miniicon.id} id={miniicon.id} ...

The name 'Map' cannot be located. Is it necessary to alter your target library?

After running the command tsc app.ts, an error occurs showing: Error TS2583: 'Map' is not recognized. Should the target library be changed? Consider updating the lib compiler option to es2015 or newer. I want the code to compile without any issu ...

Setting up Windows authentication for an ASP.NET Core 6.0 and Angular project: A complete guide

Attempting to set up Windows authentication with the ASP.NET Core 6 Angular template. Here is the current configuration in use: The following configuration has been added to the program.cs file: // Add services to the container. builder.Services.AddContr ...

Tips for extracting the image URL from my JSON string

I am in possession of a json file that includes URLs for images, and I have come across something that seems to be a URL, which is encoded as: iVBORw0KGgoAAAANSUhEUgAAADYAAAAzCAMAAADrVgtcAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6m ...

Saving data into JSON file was unsuccessful due to incorrect ordering of the entries

Currently, I am developing a desktop application using QT C++ that is responsible for converting a text file into a JSON file. Here is an example of the desired output: { "102": { "NEUTRAL": { "blend": ...

The conversion from a !DOCTYPE of type java.lang.String to a JSONObject is not possible

I am in the process of developing an Android Studio application for user login and registration. As a beginner, I encountered a JSONException issue. The code I put together is based on this tutorial. Below is the description of the error: 06-12 05:17:31 ...

What is the proper way to declare app.use using TypeScript with the node.js Express module?

I am working on a node.js app that utilizes typescript with express. In my code, I have defined an error middleware function as shown below: app.use((error:any, req:Request, res:Response, next:NextFunction) => { res.status(500).json({message:error.m ...

Encountering compilation errors with Angular project following installation of a package

I recently installed the LocalStorage Package and encountered the following message: npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Error encountered while parsing data in Internet Explorer versions 7, 8, 9, and 10 due to an invalid character. The status

This block of code is functioning correctly on Chrome and Firefox, however it seems to be having issues with Internet Explorer! It involves a simple JSON file call, fetching data, and then displaying it on an HTML webpage. Here's the code snippet: $. ...

Tips for dynamically displaying Angular Material tags within an Angular component using JSON data

I received a JSON response structured like this: { _id: '5dd0d0dc4db1cf9c77781aaa', picture: 'http://placehold.it/32x32', name: 'Graciela Mcmahon', guid: '98c0fcc2-1dfc-4974-bdae-d8263d783e0a&ap ...

Tips for implementing a personalized Circuit Breaker in Node.js that monitors request volume

Currently, I am exploring the most effective way to implement a circuit breaker based on the number of requests served in a Typescript/express application rather than fail percentage. Given that the application is expected to accommodate a large volume of ...

Angular button will be disabled if the form control is empty

Is there a way to deactivate the search button when the text box is empty? I attempted to use the "disabled" attribute on the search button, but it didn't work. Here is my HTML code: <div class="col-md-5 pl-0"> <div class="in ...

What is the method for providing a date format choice in JSON?

I am encountering an issue in my PHP script where I use JSON to pass parameters. When I pass the date as "09-09-2015", it functions correctly. However, when I try to pass the date as $date, it does not work. How can I resolve this problem? $item1 = "test ...

Unreliable TypeScript errors when using spread arguments

Consider this function: function foo(a: number, b: number) {/* ... */} Error is triggered by Snippet 1: foo(1, ...[]); Expected 2 arguments, but received only 1. Error is triggered by Snippet 2: foo(1, 2, ...[]); Expected 2 arguments, but rece ...

Leveraging arrays within a JSON Schema to handle items

I am completely new to JSON Schema and I have a specific requirement where some items in an array need to be mandatory while others are optional, each with different validation rules. These items may not necessarily appear in sequential order. Moreover, th ...

Transfer PHP variables into a JavaScript array for dynamic data population

Similar Question: Dynamic Pie Chart Data in Javascript and PHP This snippet of code is utilized for populating a pie chart using javascript: <script type="text/javascript"> var agg = { label: 'Aggressive', pct: [60, 10, 6, 30, 14 ...

Decoding a JSON string with Python

As a newcomer to parsing JSON strings, I recently utilized json.loads for text analysis but am struggling with isolating only the Titles. The code snippet demonstrates this: from alchemyapi import AlchemyAPI import json alchemyapi = AlchemyAPI() def ru ...

Searching for a specific document using AngularFirestore - what's the best method?

Is it possible to create an Observable that is limited to a single document? While the code provided creates an Observable for querying multiple documents: foo.component.ts import { AngularFirestore } from '@angular/fire/firestore'; import { O ...