0

我正在使用 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);
      }

哪个是对的。我搜索了类似的问题并尝试了答案(这里这里这里),但我一直收到同样的错误。我错过了什么?有什么想法吗?

4

1 回答 1

3

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

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

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

于 2013-07-09T09:31:39.907 回答