0

I have an array that I'm showing on a table, but I need to edit all the values in one column of that table. My code is like this:

<tr *ngFor="let line of invoice.lines; let index = index">
    <td>
        {{line.quantity}}
    </td>
    <td style="width: 35%">
        {{line.productName}}
    </td>
    <td>
        <input type="number" class="form-control" name="order_price"
        id="field_order_price" [(ngModel)]="line.price"/>
    </td>
...
</tr>

The thing is that the input has the same value in all the rows (which is the last one in the array). I tried printing the value of line.price next to the input to be sure that the I'm getting the right values and this is what I get:

correct value + wrong values

What am I missing?

UPDATE 1: changed by suggstions hasn't worked :(

<input type="number" class="form-control" name="order_price" (click)="onOrderPriceChange(index)" id="field_order_price1_{{index}}" [(ngModel)]="invoice.lines[index].price"/>

Angular:

trackByIndex(index: number, line: IInvoiceLine): any {
    return line.id;
  }
2
  • 2
    Seems to be common to experience issues with [(ngModel)] within an *ngFor directive. I've not come across this yet myself, but maybe take a look at this: stackoverflow.com/a/40315703/12431728 Commented Nov 30, 2019 at 6:17
  • I added trackBy but it hasn't change the issue Commented Nov 30, 2019 at 6:56

2 Answers 2

3

Each input should have a unique ID attribute. Let me know if you have some queries.

<tr *ngFor="let line of invoice.lines; let index = index">
  <td>
    {{line.quantity}}
  </td>
  <td style="width: 35%">
    {{line.productName}}
  </td>
  <td>
    <input type="number" class="form-control" name="{{'order_price_' + index}}" id="{{'field_order_price_' + index}}" [(ngModel)]="invoice.lines[index].price" />
  </td>
</tr>

Hope this helps :)

Sign up to request clarification or add additional context in comments.

6 Comments

almost there, I still see the '12' as value, but while inspecting I can see the correct value as 'ng-reflect-model="16.95"' why could that be?
just wanting to know have you updated the Id & [(ngModel)] as shown in the code? apparently having same ids for multiple attributes might lead to this
@Alejandro I have updated the answer, turns out name needs to be unique as well. Give it a shot
thank you. That did it, both id and name must be unique
@Ayyub, it's not necesary an unique "id", really it's not necesary use "id". The key is, obviously, give different variable to [(ngModel)]
|
2

You need to give a UNIQUE ID for each input. You can also use the below code to resolve your concern.

<tr *ngFor="let line of invoice.lines; let index = index">
  <td>
      {{line.quantity}}
  </td>
  <td style="width: 35%">
      {{line.productName}}
  </td>
  <td>
      <input type="number" class="form-control" [id]="'field_order_price' + index" [(ngModel)]="line[index]" />
  </td>
</tr>

Hope this helps! Thank you.

2 Comments

Idem comentary about "id" that before answers
noup, your comment doesn't apply to this answer. This answer is also correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.