6

所以我需要知道汽车的附加功能,而不是用户想要包含在他的偏好中。我正在尝试从通过 ajax 请求获得的数组创建输入复选框,并通过 ng-repeat 生成输入。主要目标是了解用户选择的复选框。我希望我的方法是创建一个包含所选数组的辅助数组,但我不知道如何为 ng-repeat 迭代中的每个项目设置一个唯一的 ng-model 以便我可以知道选定的列表项目。我想我的角度知识中还剩下一些东西。这是我现在所拥有的..

在控制器...

$http.get('/ajax/ajax_get_extras/'+$scope.car.version+'/false').success(function(data) {
                $scope.extras = data;
         });  
$scope.addExtra = function(){ // ... manage the auxiliar array }

在 html...

<div ng-controller="Controller">
        <form novalidate class="simple-form">
            <span ng-repeat="extra in extras">
                <input  type="checkbox" ng-model="extra.id" ng-change="addExtra()" name="extra_{{extra.id}}" >{{extra.name}} - <strong>{{extra.real_price | onlynumber | currency}}</strong>
            </span>
        </form>
</div>

而且我被卡住了,因为 extra.id 没有转换为真正的 extra.id 并保持为字符串“extra.id”>_<

我尝试了 extra_{{extra.id}}extra.id{{extra.id}}$index作为可能的 ng-model 并且没有工作。

4

1 回答 1

13

在 AngularJS 1.1.5 中,您可以在 ngRepeat 中使用“track by”。

这样你就可以:

 <input type="checkbox" ng-repeat="e in extra track by $index" ng-model="extra[$index]">

这是一个例子: http ://plnkr.co/edit/6lNo6R5EPsNGHUU6ufTE?p=preview

于 2013-07-22T12:20:16.153 回答