简体   繁体   English

将对象键与ng-model中的值绑定

[英]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' ng-repeat中的项目作为键传递给对象,以获取文本“ 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: 当您要求使用“ Angular-way”来执行此操作时,以下是Angular的ngRepeat示例的轻度修改版本:

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

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

It makes use of Angular's internal conversion of array-like objects. 它利用了Angular对类似数组的对象的内部转换。

Link to Angular's ngRepeat -documentation: https://docs.angularjs.org/api/ng/directive/ngRepeat 链接到Angular的ngRepeat文档: https ://docs.angularjs.org/api/ng/directive/ngRepeat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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