我在AngularJS新手,我讀了關於登錄和認證具有角JS教程,但我仍然搞不清楚我的代碼點多,現在我已經到了登錄和存儲在瀏覽器中的一個令牌會話,但我不能重定向到主頁在洛後, 這裏的MyService:認證機制
function authenticationSvc ($http, $q, $window, auth_uri) {
var userInfo;
function login(username, password) {
var deferred = $q.defer();
$http.post(auth_uri, {
username: username,
password: password
}).then(function(result) {
userInfo = {
accessToken: result.data.token
};
$window.sessionStorage["pallas_token"] = result.data.token;
deferred.resolve(userInfo);
},
function(error) {
deferred.reject(error);
});
return deferred.promise;
}
function getUserInfo() {
return userInfo;
}
return {
login: login,
getUserInfo: getUserInfo
};
};
,這是我的狀態配置
.state('dashboard', {
url:'/dashboard',
controller: 'HomeController',
templateUrl: 'partials/dashboard/main.html',
resolve:{
auth: function($q, authenticationSvc) {
var userInfo = authenticationSvc.getUserInfo();
if (userInfo) {
return $q.when(userInfo);
} else {
return $q.reject({ authenticated: false });
}
}
}
}
最後這個我.RUN塊:
angular
.module ('mainApp')
.run (function ($rootScope, $state, $location) {
$rootScope.$on('$stateChangeSuccess', function(userInfo) {
console.log(userInfo);
});
$rootScope.$on('$stateChangeError', function(evt, toState, toParams, fromState, fromParams, error) {
if (error.authenticated == false) {
$state.transitionTo("login");
}
});
});
請幫我解決這個問題,我需要幫助我的朋友們:(
我對失蹤後我登錄控制器對不起,有:
function LoginController($scope, $state, authenticationSvc){
$scope.submit = function(credentials){
authenticationSvc.login(credentials.username, credentials.password);
};
};
請還顯示您的登錄 – manzapanza
對不起,我已經添加的頁面的控制器還myController的。 –