我有3页A,B和C. A要求登录;
现在我到目前为止做了什么;这是我的 working plunker
将 params 添加到登录状态,并在 .config 块中设置为null
$stateProvider
.state("login", {
url: '/login',
controller: 'LoginController',
data : { pageTitle : 'Home', requiresAuth: true }
params : {'returnTo' : null }
});
当用户点击经过身份验证的页面时,我们会将用户发送到登录页面,并在$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 });
}
};
登录控制器;成功登录后,我们将参数绑定到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);
});
};
在.run
阻止我们捕获此广播事件,并通过$state.go
$rootScope.$on(AUTH_EVENTS.loginSuccess, function(event) {
$timeout(function() { $state.go($rootScope.returnTo); });
});
但我认为,这不是一种有效的方法,因为我还需要检查身份验证;我尝试使用 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' };
}
但不明白为什么这不起作用;请帮我解决这个问题
答案 0 :(得分:0)
经过各种文章的大量浏览后得到了解决方案
现在替代方法是使用 pos = pos[satIDs]
属性;删除resolve
属性,这是所有代码
在登录状态下,添加params
属性并添加resolve
属性,以便用户在登录后无法访问 / login 。
onEnter
定义 .state("login", {
url: '/login',
templateUrl: './user/login.html',
controller: 'LoginController',
controllerAs : 'vm',
resolve: { returnTo: returnTo },
onEnter: function($state, AuthService){
if(AuthService.isAuthenticated()) {
return $state.target('base', undefined, { location: false });
}
}
})
函数copied from here
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 to the original attempted "to state"
return { state: redirectedFrom.to(), params: redirectedFrom.params("to") };
}
// The user was not redirected to the login state; they directly activated the login state somehow.
var fromState = $transition$.from();
var fromParams = $transition$.params("from");
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' };
}
如何在函数内部可用!!!,有人可以解释而不是感恩
现在在 .loginController 中注入$transitions$
和$state
(函数名称),成功登录后,使用{{1}更改状态从returnTo
函数
state
值
params
希望它可以帮助某人