Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngFor with Data binding?

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?

like image 587
user3369579 Avatar asked Dec 25 '22 10:12

user3369579


2 Answers

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

like image 145
Siva Avatar answered Dec 28 '22 10:12

Siva


You could use ngModel to do this automatically

<li *ngFor="#hero of heroes">
  <input type="text" [(ngModel)]="hero.name"/>
</li>
like image 21
cexbrayat Avatar answered Dec 28 '22 09:12

cexbrayat