Angular自定义指令监听页面渲染

版权声明: https://blog.csdn.net/qq_23521659/article/details/83621852

需求:ng-repeat遍历完成后,执行函数

代码如下

js(自定义指令):

// 自定义监听页面渲染指令
app.directive('repeatFinish',function () {
    return{
        link:function (scope,element,attr) {
            // scope.index为当前遍历的位置
            console.log(scope.$index);
            if(scope.$last==true){
                // 遍历到最后一个,则scope.$last为true
                console.log(scope.$last);
                // attr.repeatFinish就是绑在指令上的函数
                console.log(attr.repeatFinish);
                // scope.$eval执行函数
                scope.$eval(attr.repeatFinish)
            }
        }
    }
})

add():

    $scope.add=function(){
       console.log("add函数执行成功。")
    }

html:

<div ng-repeat="x in relationProjects" repeat-finish="add()"></div>

执行结果:

猜你喜欢

转载自blog.csdn.net/qq_23521659/article/details/83621852