简体   繁体   English

复选框ng-click触发两次

[英]Checkbox ng-click fired twice

I am trying fill some checkboxes dynamically and use the selected items to filter the data. 我正在尝试动态填充一些复选框,并使用选择的项目来过滤数据。

My HTML 我的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>

In my controller I am using this javascript, not unlike most other examples I've seen. 在我的控制器中,我正在使用此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);
  }
};

Sadly enough I see the console logged twice , once with -1, once with 0. So I end up with no selected checkbox. 可悲的是,我看到控制台记录了两次 ,一次是-1,一次是0。所以最后我没有选中任何复选框。

When I build the app using grunt and place it on the webserver it works correctly, but when I run it using grunt serve it keeps calling the controller twice. 当我使用grunt构建应用并将其放置在网络服务器上时,它可以正常工作,但是当我使用grunt serve运行它时,它会两次调用控制器。

As Brian mentions in a comment : 正如Brian 在评论中提到的

How is your code compiled differently with grunt serve versus for production? 使用grunt服务和生产时,您的代码如何进行不同的编译? From the fact that it's being called twice in one instance but not the other points to your code being included twice when it gets concatenated via grunt serve, but not when built for prod. 从事实来看,它在一个实例中被调用了两次,但在另一实例中没有被调用,这表明当通过grunt serve将其连接时,您的代码被包含了两次,而在为prod构建时则没有。

Grunt was set differently at different environments. 在不同的环境下,Grunt的设置有所不同。 Fixing that, solved the problem. 解决该问题,即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM