【发布时间】:2016-09-07 12:17:40
【问题描述】:
我已经成功地创建了一个功能来检查一个隐藏的复选框,该复选框存在于通过 ng-repeat 生成的复选框所在行的 ng-click 上。但是,当使用 ng-change 指令选中复选框时,我还具有将该项目添加到单独数组的功能。
如果我取消隐藏元素并直接选中复选框,我的代码将完美运行,但如果我在 TR 上使用 ng-click 指令,复选框会被选中(可见)但数组不会更新。如果我再次单击它,它仍然处于选中状态,并且该项目被添加到新数组中。
这不是 ng-click 需要两次点击才能触发的问题。这是我的标记:
<tr ng-cloak ng-repeat="item in mostRecent | orderBy:sortType:sortReverse | filter: query" class="hovered" ng-class="{'hide':showReports && item.status == 'Not Started' || showReports && item.status == 'inProgress','rep-checked': bool}" ng-click="bool = !bool">
<td class="hidden"><div class="checkbox">
<label class="checkbox">
<input type="checkbox" ng-change="sync(bool, item)" ng-model="bool" ng-checked="isChecked(item)">
</label>
</div></td>
和js:
$scope.isChecked = function(id) {
var match = false;
for(var i=0 ; i < $scope.selectionData.length; i++) {
if($scope.selectionData[i].id == id){
match = true;
}
}
return match;
};
// Create a new array from the selected data
$scope.selectionData = [];
var selectionData = $scope.selectionData;
var result = selectionData.filter(function(e){ return e.id == id; });
$scope.sync = function(bool, item){
if ($scope.selectionData) {
$scope.selectionData.push(item);
}
else {
$scope.selectionData.splice(item);
}
console.log('check' + $scope.selectionData);
};
【问题讨论】:
标签: angularjs checkbox angularjs-ng-click angularjs-ng-change