我正在尝试使用angularjs和restangular。
我在使用restangular时遇到问题,因为它给了我一个错误:
TypeError: undefined is not a function
at Object.login (localhost:8000/src/common/factories/session.js:110:38)
at localhost:8000/src/common/factories/session.js:207:34
at Scope.$emit (localhost:8000/vendor/angular/angular.js:12809:33)
at Scope.$scope.Login (localhost:8000/src/common/factories/session.js:185:28)
at localhost8000/vendor/angular/angular.js:10735:21
at localhost:8000/vendor/angular/angular.js:18942:17
at Scope.$eval (localhost:8000/vendor/angular/angular.js:12595:28)
at Scope.$apply (localhost:8000/vendor/angular/angular.js:12693:23)
at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
at HTMLButtonElement.<anonymous>
(本地主机:8000 / vendor / angular / angular.js:18941:21)
我的控制器在这里:https://github.com/Seraf/LISA-WEB-Frontend/blob/master/src/common/factories/session.js#L188并调用登录功能(上面错误中的line和pos)
在这里,似乎我在上面粘贴了错误,因此在Restangular上似乎效果不佳。
由于我是angularjs的新手,也许我没有做最佳实践...我已经注入了restangular作为对我的根模块的依赖:https://github.com/Seraf/LISA-WEB-Frontend/blob/master/src/app/app.js#L6也许会引起一些问题?
[编辑]
有罪的线是:
110:“。setBaseUrl('backend / api / v1')”
207:“ $ Session.login(data);”
有罪代码:
angular.module("SessionManager", ['http-auth-interceptor','restangular'])
.constant('constSessionExpiry', 20) // in minutes
.factory("$Session", [
'$rootScope',
'$q',
'$location',
'$log',
'$http',
'constSessionExpiry',
function($rootScope, $q, $location, $log, $http, Restangular, authService, constSessionExpiry) {
return {
login: function(data){
$log.info("Preparing Login Data", data);
var $this = this;
return Restangular
.setBaseUrl('backend/api/v1')
.all('user/login/')
.post(data)
.then(function userLoginSuccess(response){
$log.info("login.post: auth-success", response);
$this.User = response;
// remove properties we don't need.
delete $this.User.route;
delete $this.User.restangularCollection;
$this.User.is_authenticated = true;
$this.cacheUser();
$this.setApiKeyAuthHeader();
$this.authSuccess();
}, function userLoginFailed(response){
$log.info('login.post: auth-failed', response);
$this.logout();
return $q.reject(response);
});
},
};
}])
谢谢您的帮助 !
最佳答案
您的依赖项注入错误:
.factory("$Session", [
'$rootScope','$q','$location','$log','$http','constSessionExpiry',
function($rootScope, $q, $location, $log, $http, Restangular, authService, constSessionExpiry) {
您正在将
constSessionExpiry
注入为Restangular
,而后两项服务则没有注入。这意味着调用
Restangular.setBaseUrl
实际上是在查找未定义的20.setBaseUrl
而不是函数-因此出错!要继续进行修复,请确保传递正确的服务名称以匹配函数参数,例如:
.factory("$Session", [
'$rootScope','$q','$location','$log','$http','Restangular','authService','constSessionExpiry',
function($rootScope, $q, $location, $log, $http, Restangular, authService, constSessionExpiry) {
关于javascript - Restangular:TypeError:undefined不是一个函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24698338/