我已经构建了一组很棒的引导程序图标,就像单选按钮一样。我需要用复选框行为做同样的错误,允许选择和存储多个选项。我似乎无法理解如何去做。
HTML
<h1>AUTOMATIC TASKS
<ul class="nav nav-pills nav-stacked user-dems">
<li ng-repeat="optionText in type.options" class="underline"><a href="#" ng-click="selectType(optionText)" class="queue-type queue-text-margin"><span ng-class="{"glyphicon-ok":type.selected===optionText,"glyphicon-unchecked":type.selected!==optionText}" class="glyphicon glyphicon-ok queue-box-padding"></span>{{optionText}}</a></li>
</ul>
</h1>
控制器
angular.module('app').controller('mvQueueListCtrl', function($scope, mvQueue) {
........
........
$scope.selectType = function(opt){
$scope.type.selected = opt
},
$scope.type = {
options: ['Fax', 'Physical', 'Digital'],
//selected: 'None'
selected: 'Fax'
},
..........
..........
..........
}
);
答案 0 :(得分:1)
您可以稍微更改视图模型以指示它已被选中,因此不是基元数组而是将其更改为对象数组: -
$scope.type = {
options: [{name:'Fax', selected:true},
{name:'Physical', selected:false},
{name:'Digital', selected:false}],
};
更新处理程序以切换复选标记。
$scope.selectType = function(opt){
opt.selected = !opt.selected;
};
只需更改ng-repeat
。
ng-click="selectType(option)"
ng-class
简化为ng-class="{'true' : 'glyphicon-ok', false:'glyphicon-unchecked'}[option.selected]"
<li ng-repeat="option in type.options track by $index">
<a href="#" ng-click="selectType(option)" class="queue-type queue-text-margin">
<span ng-class="{'true' : 'glyphicon-ok', false:'glyphicon-unchecked'}[option.selected]"
class="glyphicon glyphicon-ok queue-box-padding"></span>{{option.name}}
</a>
</li>
<强> Demo 强>