AngularJS服务:获取错误没有方法'保存'

时间:2013-07-09 09:22:06

标签: angularjs angularjs-service

我正在使用AngularJS v1.0.7,以下是我如何设置服务:

angular.module('myAngularJSApp.services',['ngResource'])
    .factory('RegisterNumber', ['$resource', function($resource) {
        return $resource( '../api/register/number/:id', {id:'@id'});
    }])
    .factory('RegisterChannel', ['$resource', function($resource) {
        return $resource( '../api/register/channel/:id', {id:'@id'});
    }])

这是我的控制器:

angular.module('myAngularJSApp')
  .controller('RegisterCtrl', ['$scope', '$location', 'RegisterNumber', 'RegisterChannel', function ($scope, RegisterNumber, RegisterChannel) {

    $scope.step = 1;
    $scope.advanceStep = function(_step){
      var AcctNum = RegisterNumber
        , AcctChn = RegisterChannel

      switch(_step){
        case 1:
          var acctNum = AcctNum.save({ // This line throws the error
                          id : $scope.security_number 
                        },
                        // SUCCESS
                        function(){
                          $scope.showError = false;
                          advance(_step);
                        },
                        // ERROR
                        function(response){
                          $scope.showError = true;
                        });
          break;
        case 2:
          var acctChn = AcctChn.save(
                        { id : $scope.channel},
                        // SUCCESS
                        function(response){
                          $scope.showError = false;
                          advance(_step);
                        },
                        // ERROR
                        function(response){
                        });
          break;
      }
    }        
  }]);

我收到了这个错误:

TypeError: Object #<Object> has no method 'save'
    at Object.$scope.advanceStep ...

我做了一些检查:console.log(AcctNum)提供了一个LocationHashbangUrl对象。但是,奇怪的是console.log(AcctChn)给出了:

function Resource(value){
        copy(value || {}, this);
      }

这是正确的。我搜索过类似的问题&amp;尝试了答案(hereherehere),但我仍然遇到同样的错误。我错过了什么?有什么想法吗?

1 个答案:

答案 0 :(得分:3)

那是因为你的注射错误了:

angular.module('myAngularJSApp')
  .controller('RegisterCtrl', 
    [
               '$scope', '$location',    'RegisterNumber', 'RegisterChannel', 
      function ($scope,   RegisterNumber, RegisterChannel) { ... }
    ]
  );

您在控制器功能签名中缺少$location参数。