AngularJS - 在我检查全选后,用 ng-click 检查复选框,它检查所有,但清除所有不是清除所有
我有一个对象列表和 Json 数组。我已经用 ng-repeat 声明了复选框,但是当我检查一个和多个复选框时 - 一个一个地选择复选框,然后如果我选择要全选的部分,则选中所有复选框,使用全选按钮,它会全选还。但是在我想取消全选之后,数组没有对象是真的。但是仍然检查复选框的可视化,为什么?谁可以帮我这个事?
这是我关于复选框的编码部分
var app = angular.module('myApp', []);
app.controller('companyCtrl', ['$scope',
function($scope) {
$scope.addCompany = function(cmpid) {
$scope.form.companies.push(cmpid);
};
$scope.Companies = [{
"id": "001",
"name": "Company1",
"address": "Bla bla street, Somewhere City, Some Country"
}, {
"id": "002",
"name": "Company2",
"address": "Bla bla street, Somewhere City, Some Country"
}, {
"id": "003",
"name": "Company3",
"address": "Bla bla street, Somewhere City, Some Country"
}, {
"id": "004",
"name": "Company4",
"address": "Bla bla street, Somewhere City, Some Country"
}, {
"id": "005",
"name": "Company5",
"address": "Bla bla street, Somewhere City, Some Country"
}, {
"id": "006",
"name": "Company6",
"address": "Bla bla street, Somewhere City, Some Country"
}];
$scope.checkAll = function() {
$scope.form.companies.splice(0, $scope.form.companies.length);
$scope.all = true;
for (var i in $scope.Companies) {
$scope.addCompany($scope.Companies[i].id);
}
console.log($scope.form.companies);
};
$scope.uncheckAll = function() {
$scope.form.companies.splice(0, $scope.form.companies.length);
$scope.all = false;
for (var i in $scope.Companies) {
$scope.addCompany('');
}
console.log($scope.form.companies);
};
)
]
};
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div>
<a href="#" ng-click="checkAll()">Select All</a>
<a href="#" ng-click="uncheckAll()">Clear All</a>
</div>
<ul>
<li ng-repeat="cmp in Companies">
<div ng-if="cmp">
<input id="{{'company-'+ $index}}" type="checkbox" ng-model="cmp.Selected" ng-click="addCompany(cmp.id)" ng-value="cmp.id" ng-checked="all || cmp.Selected" />
<label for="{{'company-'+$index}}">
<div class="title">{{cmp.name}}</div>
</label>
</div>
</li>
</ul>
</body>
</html>