0

Rather than running config at the initial stage, I would like to config something at a later stage based on some information fetched from server like this:

$http.get(url)
.success( function(response){
    angular.module('MyApp').config(['$myServiceProvider', function( $myServiceProvider){
        $myServiceProvider.enableSomething(true); // delayed !!!
    }]);
});

However the running of the body of config() function is blocked until I loaded some lazy-loaded partial. Maybe a $compile() will trigger it.

I do not understand why it is like this and how to make the config() body invoked immediately.

Thanks.

gm2008
  • 4,245
  • 1
  • 36
  • 38
  • 1
    Angular Prefixes $ and $$: To prevent accidental name collisions with your code, Angular prefixes names of public objects with $ and names of private objects with $$. Please do not use the $ or $$ prefix in your code. – pat Dec 22 '15 at 16:07
  • @pat Thanks for the reminder. – gm2008 Dec 22 '15 at 16:31

2 Answers2

1

The angular-deferred-bootstrap module does that exactly : https://github.com/philippd/angular-deferred-bootstrap

See : https://github.com/philippd/angular-deferred-bootstrap#attach-constants-to-specific-modules

Also, this question offers other options : AngularJS : Initialize service with asynchronous data

Community
  • 1
  • 1
mathieu
  • 30,974
  • 4
  • 64
  • 90
1

Would it be possible in your scenario to use $http in a module.run call and configure your service in the $http's success handler?

If it absolutely needs to happen before the module.configcall, remove ng-app from your HTML and do the bootstrapping yourself by calling angular.bootstrap as outlined in the documentation after you have fetched the file.

As far as I'm aware, there's no way of blocking the configuration phase.

Steve Klösters
  • 9,427
  • 2
  • 42
  • 53