AngularJS - Checkbox checked mit ng-click nachdem ich mit allen markiert habe, check alles, aber Clearing all ist löscht nicht alleAngularJS - Checkbox checked mit ng-click nachdem ich checked mit alle auswählen check alle aber clearing alle löscht nicht alle
Ich habe eine Liste von Objekten in und Json-Array. Ich habe die Kontrollkästchen mit ng-repeat deklariert, aber wenn ich ein oder mehrere Kontrollkästchen ankreuze - eins nach dem anderen wählt es das Kontrollkästchen und danach, wenn ich das Teil für alle auswähle, für alle Häkchen, und alle auswählen, wählt alles aus ebenfalls. Aber nachdem ich alle abwählen möchte, hat das Array keine Objekte wahr. Aber die Visualisierung der Checkbox wird immer noch überprüft warum? Kann mir jemand dabei helfen?
ist hier der Teil meiner Codierung über Kontrollkästchen
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>
i Verwenden Sie [Strich] (http://underscorejs.org/) statt für (var Element in Array) – keklikhasan
vielen Dank für Ihre erinnernden verwenden erinnert Ich für forEach Schleife verwenden. @keklikhasan –