Javascript 配置依赖于其他服务的服务angularjs
我需要知道的是,我如何在$get函数之外使用像$http这样的服务,或者这是可能的吗?我的代码将加载一个json文件,该文件提供了一个字典,我的应用程序以各种方式使用该字典。我希望用户能够自定义此字典,因此我使用jquery的extend方法允许用户向对象添加值并扩展字典。下面代码中的实例化方法处理所有这些。我希望能够这样配置我的服务Javascript 配置依赖于其他服务的服务angularjs,javascript,angularjs,Javascript,Angularjs,我需要知道的是,我如何在$get函数之外使用像$http这样的服务,或者这是可能的吗?我的代码将加载一个json文件,该文件提供了一个字典,我的应用程序以各种方式使用该字典。我希望用户能够自定义此字典,因此我使用jquery的extend方法允许用户向对象添加值并扩展字典。下面代码中的实例化方法处理所有这些。我希望能够这样配置我的服务 config(['_sys_dictionaryProvider', function(_sys_dictionaryProvider) { _sys_d
config(['_sys_dictionaryProvider', function(_sys_dictionaryProvider) {
_sys_dictionaryProvider.instansiate('config/dictionary/custom/dictionary.json');
}])
但这需要$http
服务在配置时可用,我认为不是这样。如果我将$http
服务作为$get属性的一部分,它将正常工作,如前所述,除非每次使用该服务时都必须查询网络。有没有办法在另一个服务的配置中使用一个服务
完整的代码如下,让我知道如果我需要澄清
app.provider("_sys_dictionary", ['$http',
function ($http) {
var dictionary,
DictionaryService = function () {
this.definitions = dictionary;
this.define = function (what) {
var definitions = this.definitions;
if (what instanceof Array) {
for (var i = 0; i < what.length; i++) {
definitions = definitions[what[i]];
}
return definitions;
}
return this.definitions[what];
};
};
return {
$get: function () {
return new DictionaryService();
},
instansiate: function (path) {
$http.get('config/dictionary/dictionary.json').success(function (data) {
dictionary = data;
$http.get(path).success(function (data) {
jQuery.extend(true, dictionary, data)
});
});
}
};
}
]);
app.provider(“\u sys\u dictionary)”,[“$http”,
函数($http){
var字典,
DictionaryService=函数(){
这个定义=字典;
this.define=函数(什么){
var definitions=this.definitions;
if(数组的哪个实例){
for(var i=0;i
鉴于我不相信在配置阶段使用服务是可能的,因为无法保证您使用的服务已经配置好,所以我选择了以下路线
app.provider("_sys_dictionary", function () {
var dictionary,
DictionaryService = function () {
this.definitions = dictionary;
this.define = function (what) {
var definitions = this.definitions;
if (what instanceof Array) {
for (var i = 0; i < what.length; i++) {
definitions = definitions[what[i]];
}
return definitions;
}
return this.definitions[what];
};
};
return {
$get: [
function () {
console.log(dictionary);
return new DictionaryService();
}
],
instansiate: function (path) {
jQuery.ajax({
url: 'config/dictionary/dictionary.json',
success: function (data) {
dictionary = data;
jQuery.ajax({
url: path,
success: function (data) {
jQuery.extend(true, dictionary, data);
},
async: false
});
},
async: false
});
}
};
});
app.provider(“\u sys\u dictionary”),函数(){
var字典,
DictionaryService=函数(){
这个定义=字典;
this.define=函数(什么){
var definitions=this.definitions;
if(数组的哪个实例){
for(var i=0;i
我最终使用了jquery的ajax对象,并将async改为false,因为我需要在使用服务之前准备好字典。希望这对别人有帮助。如果有人知道更好的方法,我很想知道