AngularJS根据http请求的结果设置常量
我想根据HTTP请求的结果设置module.constant 下面是我项目中的演示代码AngularJS根据http请求的结果设置常量,angularjs,angular-module,Angularjs,Angular Module,我想根据HTTP请求的结果设置module.constant 下面是我项目中的演示代码 code in app.js (function(){ var myApp = angular.module('myApp',['ui.router','ui.bootstrap']); myApp.contant("API_URL","https://www.helloworld.com/API"); myApp.run(function($rootScope.$http
code in app.js
(function(){
var myApp = angular.module('myApp',['ui.router','ui.bootstrap']);
myApp.contant("API_URL","https://www.helloworld.com/API");
myApp.run(function($rootScope.$http){
some code;
});
})();
我想根据HTTP请求的结果配置一个特殊的Id,如下所示
$http.get(API_URL + "/configTag")
.then(function success(response){
var tag = response.data.tag;
if(tag === 0) {
myApp.constant("specialID","111111111");
} else if (tag === 1) {
myApp.constant("specialID","222222222");
} else {
myApp.constant("specialID","333333333");
}
},function error(response){
});
但我对前端和AngularJS是新手。我不知道如何实现这一点?简短回答:
$http
服务不能用于创建角度常数
AngularJS框架分两个阶段运行:“配置”阶段和“运行”阶段。一旦“运行”阶段开始,就不能再配置提供程序,也不能再添加常量<代码>$http服务操作只能在“运行”阶段完成
然而,服务提供商可以提供一个恒定的承诺:
app.factory("specialIDpromise", function(API_URL, $http) {
var promise = $http.get(API_URL + "/configTag")
.then(function success(response){
var tag = response.data.tag;
if(tag === 0) {
//return to chain data
return "111111111";
} else if (tag === 1) {
return "222222222";
} else {
return "333333333";
};
}).catch(function error(response){
//return converts rejection to success
return "something";
});
//return promise to factory
return promise;
});
要在控制器中使用:
app.controller("myCtrl", function(specialIDpromise) {
specialIDpromise.then(function(specialID) {
console.log(specialID);
//Do other things that depend on specialID
});
});
config
阶段中的所有操作都需要同步(包括添加常量)。$http
服务的xhr等异步操作发生在AngularJS应用程序的运行
阶段 使用服务器端数据可以满足您的需求。您必须插入所需的模块,并运行函数以获取document.ready函数中的数据。我是这样做的,但唯一的问题是服务器是否无法访问;那么应用程序就不会启动。我将此代码放在angular.module代码之外的app.js文件中,因为它在那里无法工作,因为应用程序尚未启动
angular.element(document).ready(function () {
getConstants();
});
//Get constants from server and manually bootstraps application to the DOM
function getConstants() {
var initInjector = angular.injector(["ng"]);
var $http = initInjector.get("$http");
$http.get('urlThatRetrievesConstants').then(function (result) {
/*Below I'm setting the constants on the app module and giving them a
name, which will be used to reference in whatever modules you will use it
in */
angular.module('appModuleName').constant('constantsName', result.data);
/*Here I'm bootstrapping the app*/
angular.bootstrap(document, ["appModuleName"]);
}, function (error) {
/*Handle server errors from request*/
});
}
你犯了什么错误?一般来说要做到这一点很难。使用$http是很困难的,因为它在应用程序运行之前并不真正可用,但此时设置常量已经太晚了。为什么该值需要存储在常量中?一个更简单的解决方案是让一个服务返回一个缓存的承诺,该承诺公开常量。@ShashankAgrawal我试图将代码放入myApp.run,但注入失败error@pgreen2如果全局使用常数,根据主机名的不同,我们有不同的specialID。您可以在任何需要使用常量的地方注入服务,甚至只需在
$rootScope
上正确设置常量即可。。。正如已经指出的,在主运行循环期间,不可能设置实际的角度常数。我在我的应用程序中使用这种方法,在执行开始时异步检索应用程序的配置-我在$rootScope.config
对象中设置它。做一些类似的事情也很好:$rootScope.broadcast('App:Config',$rootScope.Config)代码>