ロジャー
ロジャー

Reputation: 357

how to load basic setting into angularjs

I am new to AngularJS and MVC.

I used to do websites in a "spaghetti" way.

Now I want to load some basic settings into my website, but not via AJAX after the DOM is loaded.

For example, Google Analytics Account ID could be defined by client at the backend system.

It should be loaded together with the page, but not by AJAX.

As my main page is index.htm, loading every data by javascript.

Without being "spaghetti" way, what is the best practice I should follow to?

Thank you.

Upvotes: 1

Views: 39

Answers (1)

DerekMT12
DerekMT12

Reputation: 1349

If your back end outputs those settings into an object on window, like..

window.appSettings = {
   setting1: 'abc'
};

In angular, you can make it injectable by putting it in an constant like:

angular.module('YourModule').constant('appSettings', window.appSettings);

Then in your services, controllers, and directives, you can use those settings:

angular.module('YourModule').controller('SomeController', ['appSettings', function(appSettings) {
   //use appSettings.setting1
}]);

Upvotes: 1

Related Questions