On a specific state user is clicking link to other state in which there is a resolve condition that checks if user has permission. How to stop changing state to new one if user hasn't permission?
Asked
Active
Viewed 3,149 times
2
-
1There are few similiar questions, check for example [this](http://stackoverflow.com/a/26702638/1679310). It should give you some idea, also, there is a working plunker. – Radim Köhler Nov 20 '14 at 12:09
-
OK. So not in resolve but on $stateChangeStart - thx – piernik Nov 20 '14 at 12:14
-
Exactly... this event could/should be the place for these "AOP" solutions – Radim Köhler Nov 20 '14 at 12:15
1 Answers
1
var routesThatRequireAuth = ['/user', '/account', '/messages'];
var hasPermisions = false;
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if($.inArray(toState.url,routesThatRequireAuth)>-1 && !hasPermisions) {
event.preventDefault();
// ...
// alert message
}
});

Maciej Osytek
- 36
- 1
- 4
-
Welcome to stackoverflow, @Maciej. Thank you for taking the time to answer. In the future, i would recommend adding an explanation of how this answers the OP's (original poster) answer. – Olivier De Meulder Sep 25 '15 at 20:37