admin管理员组文章数量:1417070
AngularJS(1) is used in my UI. I need change behavior for default page. If user is not authorized he should navigate to login page other case -> special page.
Configuring anonymous module
anonimModule.config(function ($routeProvider, anonimTemplateManager, anonimUrlManager) {
$routeProvider
.when('/', {
templateUrl: anonimTemplateManager.getLoginPath(),
controller: 'loginCtrl'
})
I can set only one time this behavior (only for login for example) but the special page is not accessible if user authorized. I tried to get access to $rootScope from when (to solve my issue) - unsuccessfully.
Is it possible to implement dynamic behavior for routing? Could I have example or idea to implement it please?
AngularJS(1) is used in my UI. I need change behavior for default page. If user is not authorized he should navigate to login page other case -> special page.
Configuring anonymous module
anonimModule.config(function ($routeProvider, anonimTemplateManager, anonimUrlManager) {
$routeProvider
.when('/', {
templateUrl: anonimTemplateManager.getLoginPath(),
controller: 'loginCtrl'
})
I can set only one time this behavior (only for login for example) but the special page is not accessible if user authorized. I tried to get access to $rootScope from when (to solve my issue) - unsuccessfully.
Is it possible to implement dynamic behavior for routing? Could I have example or idea to implement it please?
Share Improve this question asked Dec 23, 2016 at 15:35 SerhiiSerhii 7,61516 gold badges69 silver badges126 bronze badges4 Answers
Reset to default 3Try to use otherwise.
Just chain it, like so:
.when('/foo', {
templateUrl: 'foo.html',
controller: fooController
)
.when('/bar', {
templateUrl: 'bar.html',
controller: barController
)
.otherwise(
redirect: '/foo'
)
You may use state routing and then you may use statechangestart event for example:-
.state('user.dashboard', {
templateUrl: 'pages/my/dashboard/deshboard.html',
controller: 'myDashboardController'
url: '/dashboard',
access: {
loginRequired: true
}
and then in app.js you may use $stateChangeStart and some auth service that will check if user is authrize to access that state or not
$rootScope.$on('$stateChangeStart',
function (event, toState) {
var authorised;
if (toState.access !== undefined) {
authorised = authorizationServie.authorize(toState.name);
if (authorised === false) {
$state.go("loginState");
event.preventDefault();
}
}
});
You can also write $state.go('/url')
to move to another page in your anonimTemplateManager.getLoginPath()
method.
few other methods for same work
$location.url();
$state.go('/url')
$window.location.href = '/index.html';
not tried everything.
you save authorization data in local Storage when you login in application، you should check any time state change if user do not login in your application,your application must route in login page or display alert... for doing this:
var app=angular.module("myApp",["LocalStorageModule"]);
app.run(function($rootScope,$state,localStorageService){
rootScope.$on("$stateChangeSuccess", function (event, toState, parameters, from) {
if(localStorageService.get("authorizationData") !== 'logined')
$state.go("loginPage");
});
}
even you can save previous state with from variable in stateChangeSuccess event parametr and save this in rooScope for ing back to current state.
本文标签: javascriptAngular default page routingStack Overflow
版权声明:本文标题:javascript - Angular default page routing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745258724a2650255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论