AngularJS:工厂中的call promise方法

时间:2015-04-14 10:12:30

标签: angularjs angularjs-service angularjs-factory

我有一个角度工厂,但是当我尝试调用 getDepositAccountDetailsS​​ervice 时,我收到以下错误:

TypeError: this.getDepositAccountDetails.getDepositAccountDetailsService is not a function

如何在工厂内致电承诺。

  tellApp.factory('TabsFactory', function($resource){
    var activetabs = {};
    activetabs.getDepositAccountDetails = function(){
            $resource('XXXXXXX/:number', {}, {      
              getDepositAccountDetailsService: { method: 'GET', isArray: false}
            });
        }
    activetabs.setAccountInfo = function(accountnumber, result) {       
        var accountinit = {               
                accountInfo:[]                  
            };

      if(result.code == "s") {       
         this.getDepositAccountDetails.getDepositAccountDetailsService({number : accountnumber}).$promise.then(function(response){
                 return accountinit.accountInfo = response;

        }, function(error) {

        });
      }     
    }
    return activetabs;
  });

控制器

$scope.accountInfo = TabsFactory.setAccountInfo(accountnumber, $scope.result);

1 个答案:

答案 0 :(得分:1)

我认为你错过了代码中的几件事,

  1. 从服务方法getDepositAccountDetails
  2. 返回$ resource
  3. this.getDepositAccountDetails应为activetabs.getDepositAccountDetails(),因为您为工厂上下文创建了一个var。
  4. <强>工厂

    tellApp.factory('TabsFactory', function($resource) {
        var activetabs = {};
        activetabs.getDepositAccountDetails = function() {
            return $resource('XXXXXXX/:number', {}, {
                getDepositAccountDetailsService: {
                    method: 'GET',
                    isArray: false
                }
            });
        }
        activetabs.setAccountInfo = function(accountnumber, result) {
            var accountinit = {
                accountInfo: []
            };
    
            if (result.code == "s") {
                activetabs.getDepositAccountDetails().getDepositAccountDetailsService({
                    number: accountnumber
                }).$promise.then(function(response) {
                    return accountinit.accountInfo = response;
    
                }, function(error) {
    
                });
            }
        }
        return activetabs;
    });