Angular选选中和取消全选中效果

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link href="bootstrap.min.css" rel="stylesheet">
    <script src="jquery-1.9.1.min.js"></script>
    <script src="angular.min.js"></script>
    <script>
        var app = angular.module("myApp", []);
        app.controller('myCtrl', ['$scope', function ($scope) {
            //在对象为进来后加一个isSelected
            $scope.list = [
                {name:'Golde',birthday:'2000-01-10',isSelected:false},
                {name:'King',birthday:'1990-01-10', isSelected:false},
                {name:'Mark',birthday:'19890-01-10',isSelected:false},
                {name:'Marie',birthday:'2010-01-10',isSelected:false}
            ];

            $scope.checkStatus = function(){
                var str = '';
                angular.forEach($scope.list,function(value,key){
                    if(value.isSelected){
                        str += value.name+"被选中了\n";
                    }
                });

                if(str === ''){
                    str = '都未选中';
                }
                alert(str);
            };

            // 对于对象进行操作的时候(点击),会执行funcChange
            //判断对象数组中isSelected 是否为 true或false,在决定select是否为true
            $scope.selectAll = false;
            $scope.changeAll = function(){//全选/取消全选
                if($scope.selectAll==false){
                    $scope.selectAll=true;
                }else{
                    $scope.selectAll=false;
                }

                angular.forEach($scope.list,function(v,k){
                    v.isSelected = $scope.selectAll;
                })
            };

            $scope.select = false;
            $scope.funcChange = function(){// 当所有都选中时
                if($scope.select==false){
                    $scope.select=true;
                }else{
                    $scope.select=false;
                }
               
                angular.forEach($scope.list,function(v,k){
                    $scope.select = $scope.select && v.isSelected;
                });
            };

        }]);
    </script>
</head>
<body ng-app="myApp">
<div ng-controller = 'myCtrl'>
    <button ng-click="checkStatus()">查看</button>
    <input type = "checkbox" ng-model="selectAll" ng-checked="selectAll" ng-click="changeAll()"/>全选/取消全选 <br/>
    <table width="50%" class="table table-hover">
        <thead>
            <tr>
                <th>请选择</th>
                <th>姓名</th>
                <th>生日</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="obj in list">
                <td>
                    &nbsp;<input type = "checkbox" ng-model="obj.isSelected"  ng-checked="select" ng-click="funcChange()"/>
                </td>
                <td>{{obj.name}}</td>
                <td>{{obj.birthday}}</td>
            </tr>
        </tbody>
    </table>
</div>
</body>
</html>

转载于:https://my.oschina.net/chengwei426/blog/1093879

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值