0

How can I call a function when view is fully loaded (text, images and everything)?

I've tried the following in my main controller, but it's alerting before content loads.

$(window).load(function() {
 alert('dsdsds');
});

I'm using ui-router, so I tried this as well in my main controller,

$scope.$on("$stateChangeSuccess", function($currentRoute, $previousRoute) {
    alert('sasas');
}); 

Here too, it's alerting before any content loads.

How can I do this?

9
  • have you defered you javascript in your html or put it at the bottom? that should make all of the DOM load before your JS Commented Sep 6, 2015 at 11:47
  • exact which content should be loaded to fire the event you wanted? Commented Sep 6, 2015 at 12:32
  • @JoeLloyd: It's in the bottom of the main index.html Commented Sep 10, 2015 at 19:31
  • @PankajParkar: I want to alert something once my current view content is fully loaded. Commented Sep 10, 2015 at 19:32
  • @Body do you wanted to wait until ng-repeat load? or is there any specific component should be loaded before showing alert? Commented Sep 10, 2015 at 19:33

2 Answers 2

1

Try using $viewContentLoaded .It is emitted every time the ngView content is reloaded.

$scope.$on('$viewContentLoaded', function(event) {
     /...
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. I've try this, but it's alerting before content loaded. I want the alert after my view content is fully loaded.
0

You can do this if you want to watch the stateChangeSuccess DOM object to change and then do something.

 $scope.$watch('$stateChangeSuccess', function($currentRoute, $previousRoute){
    // do something
 });

1 Comment

Thank you for the reply. I've tried this, but it's alerting before content loaded. I want the alert after my view content is fully loaded.

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.