2

i have some application settings that i want to retrieve from backend, so that they would be available to all of my application controllers via injection. What is the most angular-way to do that?

1) If i only needed settings for one or few controllers i could retrieve them via routing resolve method, but what about global application scope?
2) I could use the .run() method, but since call will be async i have no guaranties that my config will be loaded before i access controllers.

Currently my settings are returned as a json object, and my templates/html files are simply served by web server. So i cannot embed anything into script tags, parse html on the server side or any similar technique.

2 Answers 2

1

I would create a service for your settings. Then using the .run() method, called a service that returns your app settings data:

angular
.module('YourApp',[])
.service('Settings',function(){
    this.data = {}
})
.run(function(Settings,$http){
    $http
    .get('/ajax/app/settings')
    .success(function(data){
        Settings.data = data
    })
})

function Controller($scope,Settings){
    // use your Settings.data
}

http://docs.angularjs.org/api/angular.Module#methods_run

Sign up to request clarification or add additional context in comments.

Comments

1

There is a neat plugin to do the job. https://github.com/philippd/angular-deferred-bootstrap

You need to change bootstrap method

deferredBootstrapper.bootstrap({
  element: document.body,
  module: 'MyApp',
  resolve: {
    APP_CONFIG: function ($http) {
      return $http.get('/api/demo-config');
    }
  }
});
angular.module('MyApp', [])
  .config(function (APP_CONFIG) {
    console.log(APP_CONFIG);
  });

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.