I
I
Ilya Oskin2016-09-11 21:14:24
Angular
Ilya Oskin, 2016-09-11 21:14:24

How to track when Angular has rendered a view?

Hello! The situation is as follows: there is a need to perform some manipulations with the dom using jQuery, the problem is that the manipulations are performed with the elements that Angular renders and if you initialize the jquery script right away, then nothing works, because Angular is late with rendering, so you have to use $timeout' You want everything to happen in the right order.
The question is how to track the moment when Angular has rendered all the elements and call the initialization of the jq script on such an event?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Aksentiev, 2016-09-11
@Sanasol

Make a separate controller and view under the piece on which jQuery needs to be run.
In this controller, run jQuery. At this point, the template is already rendered.

<div ng-include="'app/someView.html'" ng-controller="someController"></div>

_
_ _, 2016-09-11
@AMar4enko

There is an undocumented $scope.$$postDigest function that registers a callback function that will be called after the end of the $digest loop (it will work once, when the next $digest loop ends).
I usually have the following scheme:
1. An action was performed that caused the view to be redrawn (the $digest cycle began)
2. In the process of processing the action, I register the function via $$postDigest
3. All changes are fixed in the DOM, the $digest cycle is over, the function from the item 2 worked - everyone is happy

L
lega, 2016-09-12
@lega

You can try adding ng-init="runMyFunc()" to the DOM, when runMyFunc is called, the DOM should already be in place.

F
Faliah, 2016-09-13
@Faliah

In my opinion Directives and their function are linkcreated for these things. If you use components, then they have a lifecycle method$postLink()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question