复选框点击两次

时间:2015-05-15 14:32:37

标签: javascript html angularjs checkbox

我正在尝试动态填写一些复选框,并使用所选项目来过滤数据。

我的HTML

<div class="form-group" ng-repeat="category in categories">
  <label class="checkbox" >
    <input type="checkbox" ng-model="selection.ids[category.code]" id="{{category.code}}" 
           ng-click="toggleSelection(category)"
           />
    {{category.description}}
  </label>
</div>
<div class="form-group">
  <button type="submit" ng-click="filterSubmit();" class="btn btn-default">
    <span class="glyphicon glyphicon-search"></span> Filter
  </button>
</div>

在我的控制器中,我使用的是这个javascript,与我见过的大多数其他示例不同。

$scope.toggleSelection = function (category) {
  var idx = $scope.selection.indexOf(category);
  console.log('start '+ idx);

  // is currently selected
  if (idx > -1) {
    $scope.selection.splice(idx, 1);
  }
  // is newly selected
  else {
    $scope.selection.push(category);
  }
};

可悲的是,我看到控制台记录了两次,一次是-1,一次是0。 所以我最终没有选中复选框。

当我使用grunt构建应用程序并将其放在Web服务器上时,它可以正常工作,但是当我使用grunt服务器运行它时,它会一直调用控制器两次。

1 个答案:

答案 0 :(得分:0)

作为Brian mentions in a comment

  

您的代码如何使用grunt服务与生产进行不同的编译?事实上,它在一个实例中被调用了两次,但是当它通过grunt服务连接时,你的代码被包含两次,而不是在为prod构建时被包含两次。

Grunt在不同环境下的设置不同。解决这个问题,解决了这个问题。

相关问题