[英]Checkbox ng-click fired twice
我正在尝试动态填充一些复选框,并使用选择的项目来过滤数据。
我的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构建应用并将其放置在网络服务器上时,它可以正常工作,但是当我使用grunt serve运行它时,它会两次调用控制器。
正如Brian 在评论中提到的 :
使用grunt服务和生产时,您的代码如何进行不同的编译? 从事实来看,它在一个实例中被调用了两次,但在另一实例中没有被调用,这表明当通过grunt serve将其连接时,您的代码被包含了两次,而在为prod构建时则没有。
在不同的环境下,Grunt的设置有所不同。 解决该问题,即可解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.