angular1.x 指令 全选(checkbox)

本文的angular使用的1.6.1的版本,结合bootstrap的完善全选的功能
HTML:

<!DOCTYPE html>
<html ng-app='myApp'>
<head>
    <title>自己写的</title>
</head>
<link href="bootstrap.css" rel="stylesheet">
<script src="angular.js"></script>
<body ng-controller='myController'>
    <div ng-repeat='item in list' style='padding-left: 50px;border-bottom:1px solid red'>

            <div class="checkbox" >
                <input id="checkbox2-{{item.id}}" type="checkbox" 
                    hb-select-all='item.rules' ng-checked='isAllChecked' select-key='is_select' select-true-value='1' select-false-value='0' select-value-type='number'>
                <label for="checkbox2-{{item.id}}" >全选   {{item.name}}</label>
            </div>

            <div class="form-group col-sm-12" ng-repeat = 'rulelist in item.rules' >
                <div class="checkbox">
                    <input id={{rulelist.id}}  type="checkbox" ng-model="rulelist.is_select"
                           ng-true-value="1"  ng-false-value="0" >
                    <label class="control-label text-center col-sm-2" for={{rulelist.id}}>name:</label>
                </div>
                <div>{{rulelist.name}}</div>
            </div>
    </div>
</body>
</html>

解释:先定义ng-app 其次是controller名字的 一个全选的按钮,剩下的就是checkbox通过ng-reapeat来实现的

 <input id="checkbox2-{{item.id}}" type="checkbox" hb-select-all='item.rules' ng-checked='isAllChecked' select-key='is_select' select-true-value='1' select-false-value='0' select-value-type='number'>

这上面的就是指令

JS:

<script type="text/javascript">
 angular.module('myApp',[])   //入口
    .controller('myController', ['$scope', function ($scope) {   //注意方括号的写法

         $scope.list = [                //自己写的假数据 方便ng-reapeat  定义规则:is_select =>0 : 未选中
                {                                                              //is_select =>1  选中
                name:'张沪生',
                id:12,
                rules:[
                        {
                            id:1,
                            is_select:0,
                            name:'小沪生'
                        },
                        {
                            id:2,
                            is_select:1,
                            name:'大沪生'
                        },
                        {
                            id:14,
                            is_select:0,
                            name:'沪生'
                        }
                    ]
            }
        ]

    }])
            //hbSelectAll  =>  html上面的hb-select-all='item.rules'
    .directive('hbSelectAll', ['$rootScope', '$timeout','$compile',function($rootScope, $timeout,$compile) {
        return {
            restrict: 'AE',
            scope: true,
            controller: ['$scope','$element','$attrs',function ($scope, $element,$attrs) {
                console.info($attrs); //打印出来看看图一
                console.info($element) //全选的标签的按钮
                //初始化的默认值
                var selectKey=$attrs.selectKey||'is_select';
                var selectTrueValue=$attrs.selectTrueValue||true;
                var selectFalseValue=$attrs.selectFalseValue||false;
                var selectValueType=$attrs.selectValueType||'number';

                var newArr = [];
                $scope.$watch($attrs.hbSelectAll,function(newVal,oldVal){
                    console.info('newVal',newVal,'oldVal',oldVal) //自己观察的新、旧值得变化
                    $scope.isAllChecked = newVal.every(n=>{
                    //console.info(n[selectKey])
                        return n[selectKey] == selectTrueValue //every的条件 返回的true or false
                    });
                    //console.info($scope.isAllChecked)
                    newArr = newVal;
                    //console.info('newArr',newArr)
                },true)

                 $element.on('change',function () {
                    if($scope.isAllChecked){  //true
                        $scope.$apply(function () {
                            newArr.forEach(n=>{
                                n[selectKey]=selectFalseValue   //change false
                            })
                        })
                    }else{                                     // false
                        $scope.$apply(function () {
                            newArr.forEach(n=>{
                                n[selectKey]=selectTrueValue  //change true
                            })
                        })
                    }
                })
            }]
        };
    }])   
</script>

这就是$attrs的打印出来的值(图一)
这里写图片描述

源码展示:http://runjs.cn/code/dphuoo11

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值