AngularJS 服务:获取错误没有方法“保存" [英] AngularJS service: gets error has no method 'save'
本文介绍了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'});
}])
这是我的控制器:
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);
}
这是正确的.我搜索过类似的问题 &尝试了答案(此处,此处 和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) { ... }
]
);
您在控制器函数签名中缺少 $location
参数.
You're missing $location
param in controller function signature.
这篇关于AngularJS 服务:获取错误没有方法“保存"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文