我正在关注this tutorial ..我第一次从ng-click执行函数(fazerLogin),在设置cookie之后,我不知道为什么,它在$ state.go函数之前重新加载当前状态。当按钮激活fazerLogin函数(仅第一次)时,它会执行所有代码,但在我的$state.go('temperatura')
之前,它会“重新加载”相同的状态,执行我的函数initController()
并清除所有凭据。如果我再次尝试登录,它会完美运行。如果我已经登录并再次登录状态,则可以正常工作,而不会出现任何问题。有我的代码
的LoginController:
app.controller('LoginController', function($timeout, $rootScope, $location, $scope, $cookies, $state, AuthService){
(function initController() {
AuthService.LimparCredenciais();
})();
$scope.fazerLogin = function () {
if(angular.isUndefined($scope.login)){
swal("Preencha o campo 'Login'");
$('#usuario').focus();
}else if(angular.isUndefined($scope.senha)){
swal("Preencha o campo 'Senha'");
$('#senha').focus();
}else{
$scope.usuario = {login: $scope.login, senha: $scope.senha};
AuthService.Login($scope.usuario, function(response) {
if(response != null) {
if(!response.status){
swal("Login ou senha incorretos!");
}else{
//HERE IS THE PROBLEM THAT IS RELOADING CURRENT PAGE
AuthService.AtualizarCredenciais(response.usuario);
$state.go('temperatura');
}
} else {
swal("ERRO");
}
});
}
};
});
我的app.js:
var app = angular.module('app', [ 'ui.router', 'ui.materialize', 'ngResource', 'angularUtils.directives.dirPagination', 'ngCookies', 'zingchart-angularjs']);
app.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
// $urlRouterProvider.otherwise('login');
$stateProvider.state('login', {
url: "/login",
templateUrl: "pages/login.html",
controller: "LoginController"
})
});
app.run(function($cookies, $rootScope, $location, $state, $cookies, $timeout) {
$rootScope.baseUrl = window.location.origin + window.location.pathname;
// keep user logged in after page refresh
$rootScope.global = $cookies.get('global') || {};
$rootScope.$on('$locationChangeStart', function(event, next, current) {
// redirect to login page if not logged in
if ($location.path() !== '/login' && angular.isUndefined($rootScope.global.usuario)
&& $location.path() !== '/cadastro') {
$state.go('login');
}
});
});
在我的AuthService中,有我的代码:
angular.module("app").factory(
"AuthService",
function(Base64, $http, $cookies, $rootScope, $timeout, Usuario, $state) {
var service = {}
service.Login = function(usuario, callback) {
Usuario.logar(usuario)
.success(function(data) {
callback(data);
}).error(function(erro) {
// $state.go('erro');
});
};
service.AtualizarCredenciais = function(usuario) {
console.log("atualizando credenciais");
var key = Base64.encode(usuario.login + ':' + usuario.senha);
$rootScope.global = {
usuario : {
login : usuario.login,
key : key,
id : usuario.id
}
};
$cookies.put('global', $rootScope.global);
console.log('setou cookies');
//AFTER SET COOKIES, IT IS RELOADING CURRENT STATE AND EXECUTING METHOD TO CLEAR CURRENT CREDENTIALS
};
service.LimparCredenciais = function() {
console.log("limpando credenciais");
$rootScope.global = {};
$cookies.remove('global');
};
return service;
});
在我的login.html:
<div class="row">
<div class="input-field col s12">
<a ng-click="fazerLogin()" class="btn waves-effect waves-light col s12">Login</a>
</div>
</div>
在我的Usuario.js中,我的php文件只有帖子可以从数据库中获取一些数据。