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?
Try 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();
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With