1

How can I execute a function after Angular has finished rendering my template for a controller?

Here is an example of the function I am trying to run. Since the view hasn't finished rendering, there are no elements matched by $('.my-element').

function myFunction(){
  $('.my-element').css('color', 'red');
}
2
  • Are you using ng-view? Commented Sep 18, 2015 at 2:36
  • yes I am still using ng-view Commented Sep 18, 2015 at 2:37

1 Answer 1

4

You can simply listen this event if you are using ng-view.ngView-viewContentLoaded here you can get more idea about this event.

 $scope.$on('$viewContentLoaded', function(){
    //Here your view content is fully loaded !!
    //Here you can write your custom logic which will execute after content loaded.      
     $('.my-element').css('color', 'red');
  });

Register this event in your parent controller.document clearly stated

Emitted every time the ngView content is reloaded.

See demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.