Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了事件 – 当ng-repeat完成时调用函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想实现的基本上是一个“重复完成渲染”处理程序。我能够检测它什么时候完成,但我不知道如何从它触发一个函数

检查小提琴:http://jsfiddle.net/paulocoelho/BsMqq/3/

JS

var module = angular.module('testApp',[])
    .directive('onFinishRender',function () {
    return {
        reStrict: 'A',link: function (scope,element,attr) {
            if (scope.$last === truE) {
                element.ready(function () {
                    console.log("calling:"+attr.onFinishRender);
                    // CALL TEST HERE!
                });
            }
        }
    }
});

function myC($scopE) {
    $scope.ta = [1,2,3,4,5,6];
    function test() {
        console.log("test executed");
    }
}

HTML

<div ng-app="testApp" ng-controller="myC">
    <p ng-repeat="t in ta" on-finish-render="test()">{{t}}</p>
</div>

回答:
从整理工作的小提琴:http://jsfiddle.net/paulocoelho/BsMqq/4/

var module = angular.module('testApp',function ($timeout) {
    return {
        reStrict: 'A',attr) {
            if (scope.$last === truE) {
                $timeout(function () {
                    scope.$emit(attr.onFinishRender);
                });
            }
        }
    }
});

注意,我没有使用.ready(),而是包装在$ timeout。 $ timeout确保当ng-repeated元素完成渲染(因为$ timeout将在当前摘要周期结束时执行)时执行它,并且它也会在内部调用$ apply,与setTimeout不同。所以在Ng-repeat完成后,我们使用$ emit向外部范围(同级和父级范围)发出一个事件。

然后在你的控制器中,你可以抓住$ on:

$scope.$on('ngRepeatFinished',function(ngRepeatFinishedEvent) {
    //you also get the actual event object
    //do stuff,execute functions -- whatever...
});

用html看起来像这样

<div ng-repeat="item in items" on-finish-render="ngRepeatFinished">
    <div>{{item.namE}}}<div>
</div>

大佬总结

以上是大佬教程为你收集整理的事件 – 当ng-repeat完成时调用函数全部内容,希望文章能够帮你解决事件 – 当ng-repeat完成时调用函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。