shanks
shanks

Reputation: 922

At what stage in the angularjs lifecycle is the DOM completely loaded

I'm trying to wrap a native JavaScript functionality around an angularjs module and I need to bootstrap some code on DOMloaded event handler.

document.addEventListener("DOMContentLoaded",function(){
    //bootstrapper code
})

I'm a little confused if this is needed in the angular domain or not or maybe I might just be repeating something that has already been called by angular which I could have simply attached my code to. If I put the bootstrapper code in the module's run block, will it achieve the same effect, the docs says

It is executed after all of the service have been configured and the injector has been created.

with respect to the DOMLoaded Event, has it already been fired at this time?

Anyone know anything about this?

Upvotes: 0

Views: 431

Answers (1)

codef0rmer
codef0rmer

Reputation: 10530

angular.module('myApp', []).run(function($rootScope) { }); is similar to $(document).load(function() {}); in jQuery but if a part of the page is being rendered using XHR then run() method will not be helpful.

You either have to write your logic inside below event

angular.module('myApp', []).run(function($rootScope) {
   $rootScope.$on('$viewContentLoaded', function() {
      // here
   });
});

Or you better off writing a custom directive depending upon what you have to achieve.

Upvotes: 1

Related Questions