angular指令:判断ng-repeat完成后的回调事件

项目中遇到个功能点,需要在ng-repeat循环完成后才能触发,记录下。

①书写指令

    //自定义repeat完成指令
    app.directive('repeatFinish',function($timeout){
        return {
            restrict: 'A',
            link: function(scope,elem,attr){
                //当前循环至最后一个
                if (scope.$last === true) {
                    $timeout(function () {
                        //向父控制器传递事件消息
                        scope.$emit('repeatFinishCallback');
                    },100);
                }
            }
        }
    });
②控制器中接收回调事件

    //接收repeat完成事件
    $scope.$on('repeatFinishCallback',function(){
        //这里写repeat后需要进行的操作

        //demo:获取ul的高度
        var ulH = document.getElementById('tagList').offsetHeight;
        console.log('ul的高度:'+ ulH +'px');
    });
③页面中调用指令

    <ul id="tagList">
        <li ng-repeat="x in tagList" repeat-finish>{{x.name}}</li>
    </ul>
拓展知识:$emit、$broadcast和$on是各个控制器间的通信手段。

  • $emit 是由子控制器向父控制器发送事件与数据;
  • $broadcast与$emit相反;
  • $on是用于事件与数据的接收

附上完整的实例代码:

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            .tagBox{
                width: 100px;
                margin: 100px;
                border: 1px solid #000;
            }
            ul{
                overflow: hidden;
            }
            li{
                float: left;
                margin-right: 10px;
                list-style: none;
            }
        </style>
    </head>
    <body ng-app="myApp" ng-controller="myController">
        <div class="tagBox">
            <ul id="tagList">
                <li ng-repeat="x in tagList" repeat-finish>{{x.name}}</li>
            </ul>
        </div>

        <script src="angular.js"></script>
        <script>
            var app = angular.module('myApp',[]);

            //自定义repeat完成指令
            app.directive('repeatFinish',function($timeout){
                return {
                    restrict: 'A',
                    link: function(scope,elem,attr){
                        //当前循环至最后一个
                        if (scope.$last === true) {
                            $timeout(function () {
                                //向父控制器传递事件消息
                                scope.$emit('repeatFinishCallback');
                            },100);
                        }
                    }
                }
            });

            //控制器
            app.controller('myController',function($scope){
                $scope.tagList = [{
                    id: 1,
                    name: '苹果'
                },{
                    id: 2,
                    name: '香蕉'
                },{
                    id: 3,
                    name: '橘子'
                }];

                //接收repeat完成事件
                $scope.$on('repeatFinishCallback',function(){
                    //这里写repeat后需要进行的操作
    
                    //demo:获取ul的高度
                    var ulH = document.getElementById('tagList').offsetHeight;
                    console.log('ul的高度:'+ ulH +'px');
                });

            });
        </script>
    </body>
    </html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值