我试图动态填充一些复选框并使用选定的项目来过滤数据。复选框ng-click发射两次
我的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服务器运行它时,它会不断调用控制器两次。
它有助于使用ng-change而不是ng-click吗? – ABOS
没有足够的信息来复制这个问题,你发布的代码是罚款:http://plnkr.co/edit/9WT5Q1MIAgLnn8AxWJqS?p=preview – dfsq
嗨@abos,虽然我不明白为什么......确实有帮助...? – bond708