Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS复选框问题_Angularjs - Fatal编程技术网

AngularJS复选框问题

AngularJS复选框问题,angularjs,Angularjs,我是angularjs的新手,正在开发一个应用程序。我的复选框有问题。选中复选框将输出正确的数组元素,但问题是当我取消选中它们时。元素只是将它们自己添加回数组中。对不起,如果我没有解释我自己。。。我认为需要做的是从数组中删除选中的复选框(如果未选中)。我是不是错过了一个循环,还是我需要加一块$watch?。我已经试着解决这个问题好几个小时了! 如有任何建议/帮助,将不胜感激。多谢各位 Here is the code: HTML: This is the select/unselect on

我是angularjs的新手,正在开发一个应用程序。我的复选框有问题。选中复选框将输出正确的数组元素,但问题是当我取消选中它们时。元素只是将它们自己添加回数组中。对不起,如果我没有解释我自己。。。我认为需要做的是从数组中删除选中的复选框(如果未选中)。我是不是错过了一个循环,还是我需要加一块$watch?。我已经试着解决这个问题好几个小时了! 如有任何建议/帮助,将不胜感激。多谢各位

Here is the code:

HTML: This is the select/unselect on table header.

 <td class="checkbox-selector">
                                Select All
                                <input type="checkbox"  
                                       ng-model="selectedAll" 
                                       ng-click="checkAll()">
                            </td>

    ..and the individual checkboxes:



<td>
                            <input type="checkbox" 
                                   class="checkbox-row"
                                   ng-model="result.isSelected"
                                   ng-click="selectedShop(result.shopName, result.ItemId)" />
                            </td>

Angularjs:

 $scope.allSelection = [];

    $scope.checkAll = function() {
        if ($scope.selectedAll) {
            $scope.selectedAll = false;
        } else {
            $scope.selectedAll = true;
        }       
        angular.forEach($scope.results, function (result) {  
            result.isSelected = $scope.selectedAll;  
            $scope.allSelection.push("result.shopName"+"result.itemId");        

            $scope.allSelection=[];

        });
    };


$scope.selection=[];

$scope.selectedShop = function selectedShop(shopName, itemId) {
     $scope.selection.push("shopName"+ "itemId");

};
以下是代码:
HTML:这是表格标题上的选择/取消选择。
全选
..以及各个复选框:
Angularjs:
$scope.allSelection=[];
$scope.checkAll=function(){
如果($scope.selectedAll){
$scope.selectedAll=false;
}否则{
$scope.selectedAll=true;
}       
angular.forEach($scope.results,function(result){
result.isSelected=$scope.selectedAll;
$scope.allSelection.push(“result.shopName”+“result.itemId”);
$scope.allSelection=[];
});
};
$scope.selection=[];
$scope.selectedShop=函数selectedShop(shopName,itemId){
$scope.selection.push(“shopName”+“itemId”);
};

通过调试
#checkAll
函数,这个问题看起来相当简单

但乍一看,
#checkAll
的迭代部分似乎有错误——我认为您的意图是首先清除所有选择(在forEach之外),然后有条件地添加所有项(如果选中)

大概是这样的:

$scope.checkAll = function() {
    $scope.selectedAll = !$scope.selectedAll;
    $scope.allSelection=[];
    angular.forEach($scope.results, function (result) {  
        result.isSelected = $scope.selectedAll;  
        if ($scope.selectedAll) {
            $scope.allSelection.push(/*whatever needs to be in that array*/);        
        }
    });
}

另一个建议是提取不能正常工作的部分(可能在JSFIDLE上共享)。

嗨,Arturs,我想你已经解决了我的问题——清除了allSelection。我想那一定是在最后。。。如果我遇到更多的问题,我会在JSFIDLE上分享,我相信我会。。非常感谢你的帮助,非常感谢:)!!