在页面完全加载之前,我将如何处理来自服务的加载数据?
我尝试使用 resolve,但问题是我需要调用的服务尚未创建,因此我必须在 resolve 函数中执行所有服务逻辑,而我真的不想这样做.
我目前有一种处理它的工作方式,但是当然在加载视图内容和服务实际返回填充所需的数据之间存在延迟:
myApp.service('WorkshopGetStoreState', ['Transaction', 'ExpServices', 'CacheService', function(Transaction, ExpressServices, CacheService) {
return {
submit:function(defer) {
var requestTemplate = ExpServices.getRequestTemplate('WorkshopGetStoreState');
var request = requestTemplate.template;
var params = {};
params.action = "getStoreAddress";
params.locationInfo = [{'storeId': '004141' }];
for (var attrname in params) {
request.service.serviceRequest[attrname] = params[attrname];
}
Transaction.submit(requestTemplate.url, request, defer);
return defer.promise;
}
};
}]);
myApp.factory('WorkShopFlowService',['WorkShopGetStoreState', function(WorkshopGetStoreState) {
var address={};
return {
getStoreAddress : function() {
return address;
},
setStoreAddress : function(addressParam) {
address = addressParam;
},
init : function(defer){
var promise = WorkshopGetStoreState.submit(defer);
return promise;
}
};
}]);
myApp.controller('WorkshopLandingCtrl', [ '$scope', 'WorkShopFlowService', '$q', function($scope, WorkShopFlowService, $q) {
var defer = $q.defer();
var storedAddress = WorkShopFlowService.getStoreAddress();
if (storedAddress) {
$scope.address = storedAddress;
}
else {
WorkShopFlowService.getDefaultLocation(defer).then(function(result) {
$scope.address = result.serviceResponse.storeInfo[0];
WorkShopFlowService.setStoreAddress($scope.address);
});
}
}]);
编辑:似乎现在支持在较新版本的 AngularJS 中将对象引用为字符串名称,所以现在在解析中这样做是这样做的方法。