AngularJS load config on app start

后端 未结 5 1893
南方客
南方客 2020-12-12 21:38

I need to load a config file (JSON format) upon my AngularJS app startup in order to load few parameters which will be used in all api calls. So I was wondering if it is pos

5条回答
  •  天命终不由人
    2020-12-12 22:31

    I couldn't get the approach suggested my Keith Morris to work.

    So I created a config.js file and included it in index.html before all the angular files

    config.js

    var configData = {
        url:"http://api.mydomain-staging.com",
        foo:"bar"
    }
    

    index.html

    ...
    
    <% scripts.forEach( function ( file ) { %>
    <% }); %>
    

    then in my run function I set the config variables to $rootScope

    .run( function run($rootScope) {
        $rootScope.url = configData.url;
        $rootScope.foo = configData.foo;
        ...
    })
    

提交回复
热议问题