私は3ページA、B、Cを持っています。角度jsでの認証の後、要求されたページに送り返す。
- ユーザー(ユーザーがログインしているか否か)
- ユーザーがログインページよりログインしていない場合は
今私がこれまで行ってきたこと。ここで認証されたページにユーザーをクリックすると、我々は、ユーザーが送信
$stateProvider
.state("login", {
url: '/login',
controller: 'LoginController',
data : { pageTitle : 'Home', requiresAuth: true }
params : {'returnTo' : null }
});
状態をログインするのparamsを追加し、の.configブロックにnullに設定私のworking plunker
です追加のパラメータを含むログインページ
$state.target
$transitions.onBefore(requiresAuthCriteria, redirectToLogin, {priority: 10}); var requiresAuthCriteria = { to: function (state) { return state.data && state.data.requiresAuth; } }; var redirectToLogin = function($transition$, $injector) { var AuthService = $injector.get('AuthService'); if (!AuthService.isAuthenticated()) { return $state.target('login', { returnTo : $transition$.to().name }); }
};
ログインコントローラーで、ログインに成功した後、私たちは、この放送イベントをキャッチし、
$state.go
$rootScope.$on(AUTH_EVENTS.loginSuccess, function(event) { $timeout(function() { $state.go($rootScope.returnTo); }); });
しかし、私経由で要求されたページにユーザーを送る.run
ブロックでrootScope
と.broadcast
イベント
login
.controller('LoginController', function($rootScope, AUTH_EVENTS, AuthService, $stateParams){
var self = this;
self.login = function(credentials){
AuthService.login(credentials).then(function(user){
$rootScope.returnTo = $stateParams.returnTo;
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
}, function(){
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
});
};
にパラメータをバインドします私が認証を確認する必要があるので、これは有効なアプローチではないと思います。私はresolve
.state
のresolve : { returnTo : returnTo }
function returnTo ($transition$) {
var redirectedFrom = $transition$.previous();
// The user was redirected to the login state (via the requiresAuth hook)
if (redirectedFrom !== null) {
// Follow the current transition's redirect chain all the way back to the original attempted transition
while (redirectedFrom.previous()) {
redirectedFrom = redirectedFrom.previous();
}
return { state: redirectedFrom.to(), params: redirectedFrom.params("to") };
}
var fromState = $transition$.from();
var fromParams = $transition$.params("from");
console.log(fromState);
if (fromState.name !== '') {
return {state: fromState, params: fromParams};
}
// If the fromState's name is empty, then this was the initial transition. Just return them to the home state
return { state: 'base' };
}
と試みたが、これは動作していない理由を理解していません。親切に私はこの問題を解決するのに役立ち