4

How to call a simple function, after all AngularJS views loaded successfully.

function hello() {
  alert('All AngularJS works are done');
}

<!-- Below code is example about how I need a function for AngularJS loaded event -->
app.onAngularLoaded(function () { hello(); });
</script>
1

3 Answers 3

4

Try setting a timeout with a value of 0. That will happen after the current set of digest cycles.

$timeout(function(){...}, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

where I have to put this code? just inside <script> tag or $(document).ready. Am getting this error "Uncaught ReferenceError: $timeout is not defined"
You run it in an angular controller or directive or somewhere within your angular code. $timeout is an angular wrapper for timeout that gets injected if you write a controller like this: myModule.controller('MyController', function($timeout){...});
1

The problem w. what you are trying to tackle is that if you are using something like ng-include or ui-router then you won't know when those are finished loading until they are requested and loaded. It's an on-demand scenario.

You may however be looking to do something post-bootstrapping of the angular process? If that is the case you need to do something like this on you main/root angular module:

angular.module('app', [ ... any ng-dependencies here ...])
.controller(...)
.controller(...)
.directive(...)
.service(...)

//then here is where you do your post-bootstrapping logic
.run( function(){
   //code here
})

more info here - angular module docs

1 Comment

you switch between the } and ) in the end of the run function.
0

You can use directive to get it.

var homeApp = angular.module('home', ['ui.bootstrap', 'ngAnimate']);
homeApp
    .directive("pagecontent",
    function() {
        return {
            restrict: "E",
            link: function() {
                alert('All AngularJS works are done');
            },

            templateUrl: "template path",
        }

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.