Angularjs 在角坐标系中使用AND算子
我有下面的HTMLAngularjs 在角坐标系中使用AND算子,angularjs,Angularjs,我有下面的HTML <input type="checkbox" id="cbxSelectAllPreventive"/> <label>Select All</label> <ul style="list-style: none;"> <li ng-repeat="test in lists.Tests"> </li> </ul> 全选 Test是具有i
<input type="checkbox" id="cbxSelectAllPreventive"/> <label>Select All</label>
<ul style="list-style: none;">
<li ng-repeat="test in lists.Tests">
</li>
</ul>
全选
-
Test是具有isSelected属性的复杂对象数组。
我想使用复选框作为SelectAll功能。
为此,我需要向复选框提供ng模型,我可以将其作为一个方法提供,该方法在每个测试中进行检查并返回true/false。有没有什么方法可以在不编写方法的情况下进行内联操作 您是否尝试过“应该做什么”我只看到了一种方法,即使用
ng change
<input type="checkbox" id="cbxSelectAllPreventive"
ng-model="checked" ng-change="setAll(checked)"/>
...
$scope.setAll = function(isSelected){
$scope.lists.Tests.map(function(el){
el.isSelected = isSelected;
})
}
setAll
函数应负责设置此变量
$scope.setAll = function(isSelected){
$scope.selectLabel = isSelected ? 'Deselect All' : 'Select All';
$scope.lists.Tests.map(function(el){
el.isSelected = isSelected;
})
}
最后,您肯定可以选择/取消选择单个项目。在这种情况下,您必须$watch
查看您的列表。请注意第三个参数true
,它进行深入比较,否则它不会“注意”对象内部的更改
$scope.$watch('lists.Tests', function(){
var text = $scope.lists.Tests.map(function(el){ return el.isSelected; }).join();
console.log(text);
var allSelected = $scope.lists.Tests.every(function(el){ return el.isSelected;});
var noneSelected = $scope.lists.Tests.every(function(el){ return !el.isSelected;});
if(noneSelected){
$scope.selectLabel = 'Select All';
$scope.checked = false;
}else if(allSelected){
$scope.selectLabel = 'Deselect All';
$scope.checked = true;
}
if(!noneSelected && !allSelected) {
$scope.selectLabel = 'Undetermined';
// here set the check box to undetermined (third) state
}
}, true);
如果在
$scope
中没有额外功能,修改scope中的列表究竟有什么帮助?另一个附加问题是,在同一场景中,如何更改文本以选择全部/取消选择全部?我认为这不是您必须跟踪的唯一内容。显然,您将有一个选择单个项目的机制,它也应该得到反映。你必须自己建立一个新的公司。在我最新的回答中,我给出了一个什么时候去做的想法
$scope.$watch('lists.Tests', function(){
var text = $scope.lists.Tests.map(function(el){ return el.isSelected; }).join();
console.log(text);
var allSelected = $scope.lists.Tests.every(function(el){ return el.isSelected;});
var noneSelected = $scope.lists.Tests.every(function(el){ return !el.isSelected;});
if(noneSelected){
$scope.selectLabel = 'Select All';
$scope.checked = false;
}else if(allSelected){
$scope.selectLabel = 'Deselect All';
$scope.checked = true;
}
if(!noneSelected && !allSelected) {
$scope.selectLabel = 'Undetermined';
// here set the check box to undetermined (third) state
}
}, true);