使用Bootstrap Glyphicons创建Checkbox功能

时间:2014-10-13 20:06:51

标签: javascript angularjs angularjs-scope angularjs-ng-repeat

我已经构建了一组很棒的引导程序图标,就像单选按钮一样。我需要用复选框行为做同样的错误,允许选择和存储多个选项。我似乎无法理解如何去做。

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="{&quot;glyphicon-ok&quot;:type.selected===optionText,&quot;glyphicon-unchecked&quot;: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'
    },


 ..........
 ..........
 ..........

}
);

1 个答案:

答案 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