我正在开发一个角度应用程序,这个应用程序有大约 10 个可配置属性(取决于环境和客户端)。
我在 json 配置文件中有这些属性,但这真的很麻烦:每个 env/company 必须有特定的构建。所以我想在应用加载时从后端检索这些属性。
所以为了做到这一点,我创建了一个 Provider
var app = angular.module('myApp', [...]);
app.provider('environment', function() {
var self = this;
self.environment;
self.loadEnvironment = function (configuration, $http, $q) {
var def = $q.defer();
$http(...)
.success(function (data) {
self.environment = Environment.build(...);
def.resolve(self.environment);
}).error(function (err) {
...
});
return def.promise;
};
self.$get = function(configuration, $http, $q) {
if (!_.isUndefined(self.environment)) {
return $q.resolve(self.environment);
}
return self.loadEnvironment(configuration, $http, $q);
};
}
app.config(... 'environmentProvider', function(... environment) {
...
//The problem here is that I can't do environment.then(...) or something similar...
//Environment does exists though, with the available functions...
}
如何正确使用执行休息调用以填充其环境变量的此提供程序?
提前致谢!