Javascript 角度控制器在出厂前执行
我已经将一些常用代码移到了工厂。但控制器是在工厂加载之前执行的。在这种情况下,我得到空白响应(零结果) 有人能提出最好的解决办法吗 这是我的角工厂Javascript 角度控制器在出厂前执行,javascript,angularjs,promise,angular-promise,angular-resource,Javascript,Angularjs,Promise,Angular Promise,Angular Resource,我已经将一些常用代码移到了工厂。但控制器是在工厂加载之前执行的。在这种情况下,我得到空白响应(零结果) 有人能提出最好的解决办法吗 这是我的角工厂 app.factory('TabsFactory', function($resource){ var activetabs = {}; activetabs.getDepositAccountDetails = function() { return $resource('xxxx/:numb
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));
是的,这将发生,因为JavaScript执行异步操作,但是您的控制器希望事情是同步操作 调用
TabsFactory.getAccountInfo()
时,您的$resource('xxxx/:number')
可能尚未完成,并且您可以处理响应
那么,该怎么办?你已经利用了。我通常有一个存储库(带有返回承诺的方法的工厂)来处理服务器通信。以下是一个例子:
app.factory('accountRepository', ["$http","$q",function($http,$q){
return {
getDepositAccountDetails : function(id) {
var deferred = $q.defer();
$http.ger('xxx').success(deferred.resolve).error(deferred.reject);
return deferred.promise;
}
};
}] );
我的存储库将有更多的操作,如添加帐户、更新帐户信息等
然后,我的控制器/服务按如下方式调用这些方法:
accountRepository.getDepositAccountDetails(123).then(function(response) {
// Process the response..
}, function(error) {
// Some error occured! handle it
});
这样,我的代码只有在从服务器得到响应并且数据准备好使用或显示后才能执行。希望这有帮助
更新:您可能需要看一看以了解想法;) 您应该使用chain promise来更新范围变量,因为您的
accountInfo
变量是在$resource
promise中更新的
代码
TabsFactory.setAccountInfo(accountnumber, $scope.accountInfo).then(function(data){
$scope.accountInfo = TabsFactory.getAccountInfo();
alert(JSON.stringify($scope.accountInfo));
});
更新
TabsFactory.setAccountInfo(accountnumber, $scope.accountInfo).then(function(data){
$scope.accountInfo = TabsFactory.getAccountInfo();
alert(JSON.stringify($scope.accountInfo));
});
服务方法应该返回承诺,以便继续承诺链
activetabs.setAccountInfo = function(accountnumber, result) {
var accountinit = {
accountInfo: []
}
if (result.code == "v") {
//added return below
return activetabs.getDepositAccountDetails().getDepositAccountDetailsService({
number: accountnumber
}).$promise.then(function(response) {
accountinit.accountInfo = response;
return accountinit.accountInfo;
//here i am getting the JSON response
}, function(error) {
});
}
return accountinit;
}
使用诺言。。这将解决您的isuue。这是web服务的异步性质-对于AngularJS来说完全正常,但是我得到了错误,错误:TabsFactory.setAccountInfo(…)。然后undefined@vishnu很高兴帮助你,谢谢