简体   繁体   中英

How to call function after ngApp finished processing?

I have the following AngularJS module definition. I wish to run some code once this module is done processing.

var app = angular.module("Enlightks_Dashboard", ["RoomAvailabilityWidget", "TwitterNewsWidget", "BirthdayWidget", "TelephoneDataWidget", "AnnouncementWidget"]);

app.run(['$rootScope', function ($rootScope) {
        $.connection.hub.start().done(function(){});
  }]);

Per suggestion, I tried using the run block, but the callback is not being called as expected. What am I doing wrong?

you can do it in the run block. Observe the following...

var app = angular.module("Enlightks_Dashboard", ["RoomAvailabilityWidget", "TwitterNewsWidget", "BirthdayWidget", "TelephoneDataWidget", "AnnouncementWidget"]);

app.run(function() {
    console.log('boom'); // all dependent modules injected and ready
});

be sure to check out the module docs for more information

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

JSFiddle Link - simple demo with injection per your comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM