当我选中复选框时,我在控制台中收到错误。我已经附上屏幕截图。我可以在提交事件上抓取所有选中的复选框值以及如何避免无法分配给只读属性'选择'错误
angular
.module('test1')
.controller('Test1Controller', [
'$scope', '$http', '$location', '$window',
function($scope, $http, $location, $window) {
var storeid = window.localStorage.getItem("storeid");
$http.get('***').success(function(data, status, response)
{
$scope.items = (JSON.stringify(data[0].D_Services).replace(/\"/g, "")).split(',');
console.log($scope.items);
//console i'm getting ["Tyres", "Spares", "Accessories"]
$scope.selected = [];
});
$scope.check = function(data)
{
var arr = [];
for (var i in data) {
if (data[i].SELECTED == 'Y') {
arr.push(data[i].item);
}
}
console.log(arr);
$scope.selected = arr;
}
}
]);

<div ng-controller="Test1Controller" data-ng-init="loadservice()">
<div ng-repeat="item in items">
<input type="checkbox" ng-model="item.selected" ng-true-value="'Y'" ng-false-value="'N'"/> {{item}}
</div>
<input type="button" name="submit" value="submit" ng-click="check(items)"/>
</div>
&#13;
答案 0 :(得分:1)
您可以通过创建模态数据并将其附加到$ scope.items
来实现此目的var serverData = ["Tyres", "Spares", "Accessories"]; //assuming this is server data
$scope.items = [];
for (var i = 0; i < serverData.length; i++) {
var modal = {
name: serverData[i],
selected: false
};
$scope.items.push(modal);
}
更新了jsFiddle:http://jsfiddle.net/gopinathshiva/nLC3g/477/