【发布时间】:2017-01-16 14:25:56
【问题描述】:
我遇到了一个与 ng-class 相关的小问题。我有一个复选框列表。对于这个列表,我接下来设置 ng-class,如果选中了复选框,则为所选项目设置自定义 css 类。我还有一个复选框“全选”,如果我点击这个框,css类适用于所有项目,但是当我取消全选时,css类不会改变之前手动选择的项目。
我创建了plunker 来显示我的问题。
我错过了什么,我的错误在哪里?提前致谢。
html
<table class="table table-hover">
<tr ng-class="{'selected': allCategoriesSelected, 'default': !allCategoriesSelected}">
<td class="col-md-2">
<input type="checkbox" ng-click="selectAllCategories()" >
</td>
<td class="col-md-10" ng-if="!allCategoriesSelected">Select all</td>
<td class="col-md-10" ng-if="allCategoriesSelected">Deselect all</td>
</tr>
<tr ng-repeat="category in categories | orderBy : 'id'" ng-class="{'selected': allCategoriesSelected, 'default': !allCategoriesSelected}" >
<td class="col-md-2">
<input type="checkbox" ng-model="allCategoriesSelected" ng-click="updateCategory(category.id)">
</td>
<td class="col-md-10">{{ category.name }}</td>
</tr>
</table>
js
$scope.selectedCategories = [];
$scope.allCategoriesSelected = false;
$scope.selectAllCategories = function() {
$scope.allCategoriesSelected = !$scope.allCategoriesSelected;
};
$scope.updateCategory = function(categoryId) {
if ($scope.selectedCategories.indexOf(categoryId) > -1) {
$scope.selectedCategories.splice($scope.selectedCategories.indexOf(categoryId), 1);
} else {
$scope.selectedCategories.push(categoryId);
}
};
【问题讨论】:
-
plunker还有一个问题:如果选择单个类别然后选择所有类别,则类别设置正确但未选中复选框。只是 plunker 的问题还是您的代码中也有问题?
-
@Jon_Snow 固定插件
-
您希望之前选择的项目在单击“取消全选”时变为未选中状态,还是只想更改类?
-
@JoelCDoyle 我希望通过单击“取消全选”将先前选择的项目与班级一起取消选中
标签: javascript html css angularjs ng-class