将 Angular.js 与多个 html select 字段一起使用,ng-repeat(和 ng-model)

Using Angular.js with multiple html select fields, ng-repeat (and ng-model)

我正在尝试将 angularjs 与多个 html select 字段一起使用,我想将所有 selected 元素存储在一个数组中。

可能的代码如下所示:

<body ng-app="" ng-controller="myController">
{{selectedElements}}
<div ng-repeat="elem in selectedElements">
    <select 
        ng-model="elem.someThing"   <-- I do not want the someThing here
        ng-options="obj.name for obj in objects">
     </select>

    {{elem}}
</div>
<script>
    var myController = function ($scope) {
        $scope.selectedElements = [{},{}]

        $scope.objects = [
            {"id": "1234","name": "a"}, 
            {"id": "12345","name": "b"},
            {"id": "123456","name": "b"},
            {"id": "123458","name": "c"}
        ];
    };
</script>

您还可以在这里找到 fiddle:http://jsfiddle.net/dg76hetu/12/

问题是,我真的不想使用:

ng-model="elem.someThing"

因为我的 selectedElements 数组然后包含 "someThing" 作为键。 (见 fiddle) 直觉上我想做这样的事情:

ng-model="elem"

但是,这不起作用,我也知道我不应该绑定到没有点符号的模型。

所以我的问题是,如何使用 angular 来存储 "selectedElements" 数组中来自多个 select 字段的所有 selected 对象?我觉得我缺少一些基本的东西,但我就是无法让它工作。

非常感谢任何帮助。

您可以 link ng-model 使用 $index 并将 array[$index] 直接传递给 ng-model 到实际值。