Home How to do two way databinding on a specific item in an array where the array index is determined dynamically?
Reply: 0

How to do two way databinding on a specific item in an array where the array index is determined dynamically?

user1308
1#
user1308 Published in May 27, 2018, 3:38 am

Let's say I have a Javascript dynamic array coming from a database:

customers = [{'id':1, 'name':'John'},{'id':2, 'name':'Tim}, ...]

and inputs:

<input type='text' name="forJohnOnly" ng-model="customers[0].name" />

<input type='text' name="forTimOnly" ng-model="customers[1].name" />

The array comes from the database sorted and John is always the first item. While this works, I don't like it because someone might change the sort order in the database without updating the UI and the first input now holds Tim's name which is wrong. This same array is sent back to the backend to be saved in the database.

I am trying to do something like below where the correct index is determined dynamically, without creating an additional array for that purpose and copying between the two arrays

<input type='text' name="forJohnOnly" ng-model="customers[where customers.id=1].name" />
<input type='text' name="forTimOnly" ng-model="customers[where customers.id=2].name" />  (need to use id because name can be updated)

Any ideas?

Update:
While the data is in an array, the text boxes are not in a uniform layout such as a grid.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303285 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO