0

我有一个给定的对象数组,我想根据相关复选框将其对象添加到“选定”列表中。我怎么能设置它们而不必设置控制器太多。

这是使用单选框时可以正常工作的小提琴:http: //jsfiddle.net/JohannesJo/6ru2R/

JavaScript:

app = angular.module('app',[]);

app.controller('controller', function($scope){
$scope.aData = [
    {i:1},
    {i:2}
];
$scope.aSelected = [];
});

html:

<body ng-app="app">
        <div ng-controller='controller'>
            <input type = "checkbox" ng-model = "aSelected"  value = "{{aData[0]}}">
            <input type = "checkbox" ng-model = "aSelected"  value = "{{aData[1]}}">

            <div>test:     {{oSelected}}</div>
         </div>
</body>
4

1 回答 1

2

一种选择是观察oSelectedArray 上的变化并基于它创建相关对象的列表。

$scope.$watch(function () {
    return $scope.oSelected;
}, function (value) {
    $scope.selectedItems = [];
    angular.forEach($scope.oSelected, function (v, k) {
        v && $scope.selectedItems.push($scope.aData[k]);
    });
}, true);

jsfiddle:http: //jsfiddle.net/bmleite/zea7g/2/

于 2013-02-28T23:53:05.953 回答