I want to call some jQuery function targeting div with table. That table is populated with ng-repeat
.
When I call it on
$(document).ready()
I have no result.
Also
$scope.$on('$viewContentLoaded', myFunc);
doesn't help.
Is there any way to execute function right after ng-repeat population completes? I've read an advice about using custom directive
, but I have no clue how to use it with ng-repeat and my div...
Maybe a bit simpler approach with
ngInit
and Lodash'sdebounce
method without the need of custom directive:Controller:
Template:
Update
There is even simpler pure AngularJS solution using ternary operator:
Template:
Be aware that ngInit uses pre-link compilation phase - i.e. the expression is invoked before child directives are processed. This means that still an asynchronous processing might be required.
There is no need of creating a directive especially just to have a
ng-repeat
complete event.ng-init
does the magic for you.the
$last
makes sure, thatfinished
only gets fired, when the last element has been rendered to the DOM.Do not forget to create
$scope.finished
event.Happy Coding!!
EDIT: 23 Oct 2016
In case you also want to call the
finished
function when there is no item in the array then you may use the following workaroundJust add the above line on the top of the
ng-repeat
element. It will check if the array is not having any value and call the function accordingly.E.g.
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 theng-repeat
has finished rendering to the DOM before it can fire.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.
My solution was to add a div to call a function if the item was the last in a repeat.
Indeed, you should use directives, and there is no event tied to the end of a ng-Repeat loop (as each element is constructed individually, and has it's own event). But a) using directives might be all you need and b) there are a few ng-Repeat specific properties you can use to make your "on ngRepeat finished" event.
Specifically, if all you want is to style/add events to the whole of the table, you can do so using in a directive that encompasses all the ngRepeat elements. On the other hand, if you want to address each element specifically, you can use a directive within the ngRepeat, and it will act on each element, after it is created.
Then, there are the
$index
,$first
,$middle
and$last
properties you can use to trigger events. So for this HTML:You can use directives like so:
See it in action in this Plunker. Hope it helps!
I did it this way.
Create the directive
After you add it on the label where this ng-repeat
And ready with that will be run at the end of the ng-repeat.