查看
<div ng-app="checkbox" ng-controller="homeCtrl">
<div ng-repeat="item in list">
<input type="checkbox" ng-model="item.checked"
ng-click="fnChangeAssetType1(item)" />
<label>{{item.value}}</label>
</div>
<br/><br/>
New List<br/><br/>
<div ng-repeat="items in lists">
<input type="checkbox" ng-model="items.checked"
ng-click="fnChangeAssetType(items)" />
<label>{{items.value}}</label>
</div>
</div>
JS
var app = angular.module('checkbox', []);
app.controller('homeCtrl', function($scope) {
var list = [{
"id": 1,
"value": "apple",
}, {
"id": 3,
"value": "orange",
}, {
"id": 5,
"value": "pear"
}];
$scope.list=list;
$scope.lists=list;
$scope.fnChangeAssetType = function (items) {
angular.forEach($scope.lists, function (item) {
item.checked = false;
});
items.checked = true;
};
$scope.fnChangeAssetType1 = function (items) {
angular.forEach($scope.list, function (item) {
item.checked = false;
});
items.checked = true;
};
});
答案 0 :(得分:3)
这是因为您$scope.list
&amp; $scope.lists
两者都指向内存中的相同数组,因为数组(list
)是引用类型。您需要做的是为$scope.lists
创建一个单独的数组,如下所示。
您需要使用angular.copy()
$scope.list=list;
$scope.lists= angular.copy(list); // this will create a deep copy of list array and store it in another memory slot, //refer the angular.copy doc
这是更新后的fiddle