Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*ngFor how to bind each item in array to ngModel using index

Tags:

angular

ngfor

===final updated==

http://plnkr.co/edit/WKRBB7?p=preview

since I use ngModel in a form, I must add name attribue.

and my mistake is that I used same value as its name.

<form #myform="ngForm">     <table>       <tr *ngFor="let staff of staffs">          <td><input name="name" [(ngModel)]="staff.name">{{staff.name}}</td>       </tr>     </table> </form> 

after I change to belows, my problem is resolved.

<form #my2form="ngForm">    <table>       <tr *ngFor="let staff of staffs;let i = index">          <td><input name="staff.{{i}}.name" [(ngModel)]="staff.name">{{staff.name}}</td>       </tr>     </table> </form> 

==========

sorry, I can't remember why I use names[$index].Name instead of x.Name.

maybe years ago I meet some mistake using x.Name, and then made a habit of using index.

---updated-----

I need a inline edit table, two-way binding.

<table>    <tr *ngFor="let x of names">      <td><input [(ngModel)]="x.Name">{{x.Name}}</td>    </tr> </table>  let names = [ { Name:'jim'}, { Name:'tom'} ]; 

initially the pages shows:

blank text field; jim  blank text field; tom 

after I type 'aaaaaa' in the first text field, it becomes:

aaaaaa; aaaaaa  blank text field; tom 

I think the page initially would show:

jim; jim tom; tom 

so, my problem exactly is, why the initial value is missing?

like image 222
Jinceon Avatar asked Jan 14 '17 16:01

Jinceon


People also ask

What does [( ngModel )] do?

The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.

Is ngModel property binding?

NgModel is a directive that creates the FormControl instance from a domain model and binds it to a form control element. It uses two kinds of binding: Property binding.

Can't bind to ngModel since it isn't a known property of input Ng?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.


2 Answers

It should be [ngModel]="..."

<table>    <tr *ngFor="let x of names;let i = index;">      <td>{{ i+ 1 }}</td>      <td><input [(ngModel)]="names[i].Name">{{x.Name}}</td>    </tr> </table> 
like image 158
Günter Zöchbauer Avatar answered Sep 30 '22 17:09

Günter Zöchbauer


in you case sir if you are using *ngFor for loop then i don't think so you need index. why don't you just use x.Name. here is the modified code.

<table>        <tr *ngFor="let x of names;let i = index;">          <td>{{ i+ 1 }}</td>          <td><input [(ngModel)]="x.Name">{{x.Name}}</td>        </tr>     </table> 

or can you try this

<table>            <tr *ngFor="let x of names;let i = index;">              <td>{{ i+ 1 }}</td>              <td><input [value]="x.Name" [(ngModel)]="x.Name">{{x.Name}}</td>            </tr>         </table> 
like image 26
himanshu Avatar answered Sep 30 '22 19:09

himanshu