简体   繁体   中英

Binding Object key with value in ng-model

I have a object which holds the key and value pair.

$scope.groups=  {
    1050 : 'Test',
    1850 : 'Test1'
}
$scope.AnotherArray = [1050,1850];

item from ng-repeat is passed to the object as key to obtain the text 'Test'

<div ng-repeat="item in AnotherArray">
    <input type="text" ng-model="groups[item]" />
</div>

Is there a way in angular to do this ?

As you were asking for an "Angular-way" to do this, here is a mildly modified version of Angular's ngRepeat example:

<div ng-repeat="(key, obj) in groups">
  [{{key}}] {{obj}}
  <input type="text" ng-model="obj"/>
</div>

http://plnkr.co/edit/0IRvLZpbUZUQBXOnebOt?p=preview

It makes use of Angular's internal conversion of array-like objects.

Link to Angular's ngRepeat -documentation: https://docs.angularjs.org/api/ng/directive/ngRepeat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM