1

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.

1 Answer 1

1

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
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

how should I output this file? window.appSettings = {setting1: 'abc';} I can think of PHP writing into a JS, since I need these settings from database.
That's exactly how you would do it. Instead of 'abc', just use php to output your value. Setting those would just be in a script tag in your main php file.

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.