4

I've got a *ngFor where I fetch a heroes list. Now, If I change a value of hero, my heroes should be changed too, how do you do it, the best way...

     <li *ngFor="#hero of heroes">
      <input type="text" [value]="hero.name"/>
    </li>

I only know the way to make a (change)="UpdateListByItem(item)" , to call a methode, but isn't there a way to make a two way databind for all items?

user3369579
  • 486
  • 3
  • 7
  • 22

3 Answers3

7

You can do two way databinding using ngModel directive

<li *ngFor="#hero of heroes">
  <input type="text" [(ngModel)]="hero.name"/>
</li>

Whenever you change the input text the corresponding hero name will get changed.

More information can be found here https://angular.io/docs/ts/latest/guide/forms.html

Siva
  • 2,735
  • 16
  • 14
2

You could use ngModel to do this automatically

<li *ngFor="#hero of heroes">
  <input type="text" [(ngModel)]="hero.name"/>
</li>
cexbrayat
  • 17,772
  • 4
  • 27
  • 22
1

Look at this Answer. ngModel allows to change individual name and update the list immediately.

    <ul>
        <li *ngFor="#hero of heros">
            <input type="type"  [value]="hero.name"  [(ngModel)]="hero.name" />
        </li>
    </ul>
micronyks
  • 54,797
  • 15
  • 112
  • 146