gpt4 book ai didi

javascript - Angular - TypeError : XX is not a function

转载 作者:可可西里 更新时间:2023-11-01 02:32:38 24 4
gpt4 key购买 nike

也许我缺少某种属性,但我正在关注这个 project我在我的 Controller 中收到此错误。

TypeError: loginService.signin is not a function

这是我的 controller.js

angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService',  
function ($rootScope, $scope, $http, loginService) {

$scope.signin = function() {

console.log("username: " + $scope.username);
console.log("pass: " + $scope.password);

var formData = {
username: $scope.username,
password: $scope.password
};

// ERROR IN THIS LINE
loginService.signin(formData, function(res) {
console.log("asdf");
if (res.type == false) {
console.log(res.data);
} else {
$localStorage.token = res.data.token;
console.log("Window location /");
}
}, function() {
$rootScope.error = "Error en el logueo del usuario";
});

// Setting Token:
$scope.token = $localStorage.token;
}

}])

这是我的 service.js

'use strict';

angular.module('app-services', [])
.factory('loginService', ['$http', '$localStorage', function ($http, $localStorage) {
console.log('services - loginService');

var baseUrl = "http://angular-restful-auth.herokuapp.com";

function changeUser(user) {
console.log('1');
angular.extend(currentUser, user);
}

function urlBase64Decode(str) {
console.log('2');
var output = str.replace('-', '+').replace('_', '/');
switch (output.length % 4) {
case 0:
break;
case 2:
output += '==';
break;
case 3:
output += '=';
break;
default:
throw 'Illegal base64url string!';
}
return window.atob(output);
}

function getUserFromToken() {
console.log('3');
var token = $localStorage.token;
var user = {};
if (typeof token !== 'undefined') {
var encoded = token.split('.')[1];
user = JSON.parse(urlBase64Decode(encoded));
}
return user;
}

// Set user token.
var currentUser = getUserFromToken();

return {
save: function(data, success, error) {
$http.post(baseUrl + '/signin', data).success(success).error(error)
},
signin: function(data, success, error) {
$http.post(baseUrl + '/authenticate', data).success(success).error(error)
},
me: function(success, error) {
$http.get(baseUrl + '/me').success(success).error(error)
},
logout: function(success) {
changeUser({});
delete $localStorage.token;
success();
}
};
}

]);

我的应用程序一直有效,直到用户按下表单中名为 signin() 的提交按钮。然后我在控制台中得到了这两行正确的数据

>> username: somename
>> pass: somepassword

然后错误就出现了。谁能帮我传递这个 signin() 函数?

最佳答案

你弄乱了依赖注入(inject)数组,当你在函数中使用它时,它应该遵循正确的依赖顺序。

代码

angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService',  
function ($rootScope, $scope, $http, $location, $localStorage, loginService) {

关于javascript - Angular - TypeError : XX is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com