【发布时间】:2015-06-20 22:33:33
【问题描述】:
我已将一些通用代码移至工厂。但控制器在工厂加载之前正在执行。在这种情况下,我得到的是空白响应(零结果)
谁能提出最佳解决方案。
这是我的角度工厂,
app.factory('TabsFactory', function($resource){
var activetabs = {};
activetabs.getDepositAccountDetails = function() {
return $resource('xxxx/:number', {}, {
getDepositAccountDetailsService: {
method: 'GET',
isArray: false
}
});
}
activetabs.getAccountInfo = function(){
return accountinit.accountInfo;
}
activetabs.setAccountInfo = function(accountnumber, result) {
var accountinit = {
accountInfo: []
}
if (result.code == "v") {
activetabs.getDepositAccountDetails().getDepositAccountDetailsService({
number: accountnumber
}).$promise.then(function(response) {
accountinit.accountInfo = response;
//here i am getting the JSON response
}, function(error) {
});
}
return accountinit;
}
return activetabs;
});
控制器,
TabsFactory.setAccountInfo(accountnumber, $scope.accountInfo);
$scope.accountInfo = TabsFactory.getAccountInfo();
alert(JSON.stringify($scope.accountInfo));
【问题讨论】:
-
使用 promise.. 它会解决你的问题。
-
这就是 web 服务的异步特性——对于 angularjs 来说完全正常
标签: javascript angularjs promise angular-promise angular-resource