How to set AngularjJS base URL dynamically based on fetched environment variable?

后端 未结 4 1149
耶瑟儿~
耶瑟儿~ 2020-12-10 05:18

I have a development and production environment in which my URL\'s differ:

production:

www.exmaple.com/page

development:

d

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 05:46

    asumming you created a service that gets configuration object from server

    app.run(function($rootScope, yourService){   
        yourService.fetchConfig(function(config){
            $rootScope.baseUrl = config.baseUrl;
       })
    });
    

    markup

    
    
       
       ......
    

    note

    • If your service uses $http or $resource you should be fine
    • If instead you use jQuery ajax calls you should run $rootScope.$apply() after setting variables to the scope.

提交回复
热议问题