我试图摆脱 Angular 中的错误,但我不太清楚如何解决这个问题。我在 AngularJS 中有一个全选复选框和一个也有一个复选框的项目列表。这个想法是可以单独检查项目或使用选择/取消选择所有按钮。这个想法是当我说检查了两个项目并且我想在之后按下全选按钮时,因为也许我不想经历选择所有项目的所有麻烦,我得到一个不允许重复的错误。有谁知道如何解决这个问题?
这是我的 JS 函数:
$scope.selectAll = function(modelItemsList){
console.log($scope.modelItemsList);
$rootScope.modelItemsList.allItemsSelected = !$rootScope.modelItemsList.allItemsSelected;
console.log($rootScope.modelItemsList.allItemsSelected);
if($rootScope.modelItemsList.allItemsSelected){
for (var i = 0; i < $scope.modelItemsList.length; i++) {
$rootScope.temp.push($scope.modelItemsList[i].name);
console.log($scope.modelItemsList[i].name);
$scope.modelItemsList[i].isChecked = $rootScope.modelItemsList.allItemsSelected;
console.log($scope.modelItemsList[i].isChecked);
console.log($rootScope.modelItemsList.allItemsSelected);
}
}
else if (!$rootScope.modelItemsList.allItemsSelected){
for (var i = 0; i < $scope.modelItemsList.length; i++) {
$scope.modelItemsList[i].isChecked = $rootScope.modelItemsList.allItemsSelected;
$rootScope.temp = [];
console.log($scope.modelItemsList[i].isChecked);
}
}
}
这是我的html:
<md-content class="md-padding autocomplete" layout="column">
<md-chips ng-model="$root.items" md-max-chips="5">
<md-autocomplete
md-selected-item="selectedItem"
md-search-text="searchText"
md-items="item in querySearch(searchText)"
md-item-text="item.name"
md-no-cache="true"
placeholder="PU">
<span md-highlight-text="searchText">{{item.name}}</span>
</md-autocomplete>
<md-chip-template>
<strong>{{$chip}}</strong>
</md-chip-template>
</md-chips>
</md-content>
全选按钮的代码:
<div class="md-list">
<md-checkbox ng-model="modelItemsList.allItemsSelected"
ng-change="selectAll(modelItemsList)">
{{"Alle markieren" | translate}}
</md-checkbox>
<md-list> <md-list-item class="md-3-line"
ng-repeat="modelItem in modelItemsList | filter:searchText >
<div class="md-list-item-text">
<md-checkbox ng-model="modelItem.isChecked " aria-label="Checkbox 1" ng-change="selectModelItem(modelItem)">
<h3>{{ $eval('modelItem.'+propertyName) }}</h3>
<p>{{ $eval('modelItem.'+propertyDesc) }}</p>
</md-checkbox>
</div>
提前致谢!