AngularJS服务:被错误没有方法“拯救” [英] AngularJS service: gets error has no method 'save'

查看:307
本文介绍了AngularJS服务:被错误没有方法“拯救”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS v1.0.7,这里是我如何设置的服务:

I'm using AngularJS v1.0.7 and here's how I setup the service:

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

这是我的控制器:

and here's my controller:

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)给出:

I did some inspections: console.log(AcctNum) gives a LocationHashbangUrl object. However, strangely enough console.log(AcctChn) gives:

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

这是正确的。找遍了类似的问题和放大器;试过的答案(这里,<一个href=\"http://stackoverflow.com/questions/14286713/angular-resource-update-method-not-found\">here,和这里),但我不断收到同样的错误。我错过了什么?有什么想法?

which is correct. I have searched similar questions & tried the answers (here, here, and here) but I keep getting the same error. Did I miss something? Any thoughts?

推荐答案

这是因为你有错注:

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

您错过 $位置参数的控制器功能的签名。

You're missing $location param in controller function signature.

这篇关于AngularJS服务:被错误没有方法“拯救”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆