我的应用有这个.run
.run(function ($rootScope, $http) {
$rootScope.server = "http://127.0.0.1:5000/rawDemo";
var call = $rootScope.server + "/frontEnd/GetStructure";
var texts = {};
texts.languages = {};
$http.get(call).then(function (response) {
for (var i = 0; i < response.data.languages.length; i++) {
texts.languages[response.data.languages[i].iso2] = {
'title': response.data.languages[i].title,
'description': response.data.languages[i].description,
'keywords': response.data.languages[i].keywords,
'frontEndTexts': response.data.languages[i].frontEndTexts
};
}
$rootScope.texts = texts;
$rootScope.webshop = response.data;
$rootScope.webshop.language = response.data.culture.language;
$rootScope.webshop.numberFormat = "";
$rootScope.carouselData = response.data.frontEndConfig.customConfiguration.mjCarousel;
console.log('end run');
});
})
。我的一些解析器在服务中执行呼叫......
angular
.module('app')
.factory('Products',['$http', '$rootScope', function($http, $rootScope){
return {
listByCategories : function (categories){
console.log('begin service');
var call = $rootScope.server + '/products/list/categories/' + categories.fullPath +'?page1&recordsPerPage=2' ;
return $http.get(call).then(function(response) {
return response.data;
});
}
}
}])
我在console.log中的预期结果应该是: - 结束 - 开始服务
但是..begin服务在结束运行之前开始。这是因为.run完成并保持$ http异步执行并继续进行下一阶段,例如解析器。
根据我在这个网站上的研究,不可能使$ http作品同步。所以,我的问题是......关于如何处理场景的任何提示?我的服务取决于必须先加载的数据。
我无法“合并”.run和服务,因为所有其他视图服务必须先执行.run。在我的.run中,我基本上加载了十几个全局配置。
更新:我仍然被困......但我正在尝试一些事情......我改变了我做事的方式......
所以,在我的.run
上$rootScope.loadWebshop = function(callBack) {
if($rootScope.loaded) { return ; }
var call = $rootScope.server + "/frontEnd/GetStructure";
var texts = {};
texts.languages = {};
$http.get(call).then(function (response) {
for (var i = 0; i < response.data.languages.length; i++) {
texts.languages[response.data.languages[i].iso2] = {
'title': response.data.languages[i].title,
'description': response.data.languages[i].description,
'keywords': response.data.languages[i].keywords,
'frontEndTexts': response.data.languages[i].frontEndTexts
};
}
$rootScope.texts = texts;
$rootScope.webshop = response.data;
$rootScope.webshop.language = response.data.culture.language;
$rootScope.webshop.numberFormat = "";
$rootScope.carouselData = response.data.frontEndConfig.customConfiguration.mjCarousel;
$rootScope.loaded = true;
console.log('end .run');
callBack();
});
}
..在我的服务上:
listByCategories : function (categories){
return $rootScope.loadWebshop (function () {
console.log('begin service');
var call = $rootScope.server + '/products/list/categories/' + categories.fullPath +'?page1&recordsPerPage=2' ;
return $http.get(call).then(function(response) {
return response.data;
});
});
}
现在我面临着一个不同的问题......它的返回是未定义的,因为它是异步的,即使是使用前提。任何线索??