ng-repeat finish event

前端 未结 15 2836
盖世英雄少女心
盖世英雄少女心 2020-11-22 02:15

I want to call some jQuery function targeting div with table. That table is populated with ng-repeat.

When I call it on

$(document).r         


        
15条回答
  •  没有蜡笔的小新
    2020-11-22 02:59

    Here's a simple approach using ng-init that doesn't even require a custom directive. It's worked well for me in certain scenarios e.g. needing to auto-scroll a div of ng-repeated items to a particular item on page load, so the scrolling function needs to wait until the ng-repeat has finished rendering to the DOM before it can fire.

    thing: {{ thing }}

    myModule.controller('MyCtrl', function($scope, $timeout){
        $scope.things = ['A', 'B', 'C'];
    
        $scope.fireEvent = function(){
    
            // This will only run after the ng-repeat has rendered its things to the DOM
            $timeout(function(){
                $scope.$broadcast('thingsRendered');
            }, 0);
    
        };
    });
    

    Note that this is only useful for functions you need to call one time after the ng-repeat renders initially. If you need to call a function whenever the ng-repeat contents are updated then you'll have to use one of the other answers on this thread with a custom directive.

提交回复
热议问题