angularJs全选复选BUG求助(先单选后全选反选后,再进行单选之前的值未清空)

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>

    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>

</head>

<body ng-app="app">
    <div ng-controller="myCtrl" class="container bg-color">
        <section>
            全选: <input type="checkbox" ng-model="master" ng-click="all(master,user)">
            <div ng-repeat="z in user">
                <input id={{z.id}} type="checkbox" ng-model="x" ng-checked="master" ng-click="checkd(z,x)">{{z.name}}
            </div>
            <button href="#" class="btn btn-danger" ng-click="delete()">批量操作</button>
            <span>{{choseArr}}</span> 
        </section>
    </div>
    <script>
        var app = angular.module('app', []);

        app.controller('myCtrl', function($scope){
            $scope.user=$scope.user=[{
                "id": "1",
                "code": "1",
                "name": "黄"
              },
              {
                "id": "2",
                "code": "2",
                "name": "红"
              },
              {
                "id": "3",
                "code": "12",
                "name": "绿"
              }];

            $scope.choseArr = [];
            var str = ""; 
            var len = $scope.user.length;
            var flag = '';
            $scope.x = false;
            $scope.all = function (c, v) {
                if (c == true) {
                    $scope.x = true;
                    var rst = [];
                    angular.forEach(v, function(data,index,array){
                        rst.push(data.id);
                    });
                    $scope.choseArr = rst;
                    flag = 'a';
                } else {
                    $scope.x = false;
                    $scope.choseArr = [];
                    flag = 'b';
                }

            };
            $scope.checkd = function(z, x) {
                if(flag == 'a') {

                    str = $scope.choseArr.join(',') + ',';
                }
                if(x == true) {
                    str = str + z.id + ',';
                    flag = 'c'
                    if($scope.choseArr.length == len - 1) {
                        $scope.master = true
                    }
                } else {

                    str = str.replace(z.id + ',', '');
                }

                $scope.choseArr = (str.substr(0, str.length - 1)).split(',');
                var dex = $scope.choseArr.indexOf("");
                if(dex >= 0) {
                    $scope.choseArr.splice(dex, 1);
                };
                if($scope.choseArr.length == 0) {
                    $scope.master = false
                };

            };
            $scope.delete = function() {

                if($scope.choseArr[0] == "" || $scope.choseArr.length == 0) {
                    alert("请至少选中一条数据在操作!")
                    return;
                }
                else{
                        console.log($scope.choseArr)    
                }

            };

        });
    </script>

</body>



1个回答

你的str不是存着上一次的值,反选的时候清空str的值

还有别人帮你解决的问题记得及时采纳。。http://ask.csdn.net/questions/345074

            $scope.all = function (c, v) {
                if (c == true) {
                    $scope.x = true;
                    var rst = [];
                    angular.forEach(v, function (data, index, array) {
                        rst.push(data.id);
                    });
                    $scope.choseArr = rst;
                    flag = 'a';
                } else {
                    $scope.x = false;
                    $scope.choseArr = [];
                    str = "";///////
                    flag = 'b';
                }

            };
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!