【问题标题】:angular controller is executing before factory complete角度控制器在工厂完成之前执行
【发布时间】: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


【解决方案1】:

你应该使用链式promise来更新范围变量,因为你的accountInfo变量在$resourcepromise中被更新了。

代码

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;
}

【讨论】:

  • 但我收到错误,错误:TabsFactory.setAccountInfo(...).then undefined
  • @vishnu do 很高兴为您提供帮助,谢谢
【解决方案2】:

是的,这会发生,因为 JavaScript 执行异步操作,但您的控制器以这样一种方式期望事情是同步操作。

当您致电TabsFactory.getAccountInfo() 时,您的$resource('xxxx/:number') 可能仍未完成,您可以处理回复!

那么,该怎么办?你已经使用了promise。我通常有一个存储库(具有返回承诺的方法的工厂)来处理服务器通信。这是一个例子:

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
 });

这样做,我的代码只有在我从服务器得到响应并且数据准备好供使用或显示后才会执行。希望这会有所帮助..

更新:您可能想看看this 了解一下;)

【讨论】:

    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多