Reputation: 863
I have the following routes, using Angular ui-router.
If I navigate to /3872
that gives me a user with that ID and the correct template, that's great.
But the problem is, if I type an ID that doesn't exist, such as /1111
I want to redirect to the root
. At the moment it stays on the user
route and gives me an empty template instead.
$stateProvider
.state('root', {
url: '/',
templateUrl: 'home.html',
controller: 'home_controller'
})
.state('user', {
url: '/:userId',
templateUrl: 'user.html',
controller: 'user_controller'
});
$urlRouterProvider.otherwise('/');
Upvotes: 2
Views: 837
Reputation: 1241
In user_controller($stateParams,$state):
if($stateParams.userId not in userIds) {
$state.go('root');
}
Upvotes: 0
Reputation: 1243
One way is to use $stateChangeStart
event to make some filtering. That goes into app.run(...) section
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.name == "user") {
//some logic... and most probably
//event.preventDefault();
//$state.go(toState.name, toParams);
}
});
Upvotes: 1