Gatekeeper
Gatekeeper

Reputation: 1616

AngularJS - load values from api in module config block

I have a Facebook login button in my app that uses 'angular-facebook' module. In my .config block I have following code:

FacebookProvider.init('myAppId');

Now I have service that is loading myAppId from DB via REST api but I dont know how to correctly load it and pass it in .config block.

I spent last 2 hours reading manuals angd googling how to do this but I am still confused how to make this work.

Upvotes: 0

Views: 139

Answers (2)

Moncef Hassein-bey
Moncef Hassein-bey

Reputation: 1361

You can retreive the $http service by using angular injector, and then, configure your app, something like this :

(function() {


     angular.element(document).ready(function() {
           var initInjector = angular.injector(["ng"]);
           var $http = initInjector.get("$http"); 

            $http.get("/myFbAppId.json").then(function(response) {
               angular
                  .module('myApp')
                  .config(function(facebookProvider){
                      FacebookProvider.init(response.data.myAppId);
                  })  

                  angular.bootstrap(document, ['myApp']);
            })

     });

}());

Upvotes: 1

Henri Benoit
Henri Benoit

Reputation: 725

Only providers and constants can be injected into configuration blocks in order to prevent instantiating services before they have been fully configured. You basically can only inject providers and constants in your config block. So the only solution is not to do it in a config block but in a run block.

Upvotes: 0

Related Questions