Is it possible to "set" the view when a user accesses a missing state? I wanted the 404 handler to only some notice if the user tried to access a non-existent page instead of a redirecting the user.
Something similar to Github's 404.
Is it possible to "set" the view when a user accesses a missing state? I wanted the 404 handler to only some notice if the user tried to access a non-existent page instead of a redirecting the user.
Something similar to Github's 404.
As @cshion
said you can catch $stateNotFound
and go to another state
or something you want.
app.run(['$rootScope', '$state', function ($rootScope, $state) {
$rootScope.$on("$stateNotFound", function (event, unfoundState, fromState, fromParams) {
$state.go('my404state');
});
}]);
This will work only by invoking $state.go('missingstate');
but if the user types a fake url not work. so is more suitable for testing/debugging purposes.
In the other hand you can redirect the user using $urlRouterProvider.otherwise
app.config( ['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider
.when('/', [$state, function($state) { $state.go('home') }])
.otherwise('/notfound');
}
]);
And create a specific state to show 404 not found.
Another option is to use $urlRouterProvider.rule
for custom url handling
app.config(['$urlRouteProvider','MyAuthService', function ($urlRouterProvider) {
$urlRouterProvider
.when('/', [$state, function($state) { $state.go('home') }])
.rule(function ($injector, $location) {
if (MyAuthService.isAuthenticated()) {
return "/user/mainpage";
}
return $location.path();
})
.otherwise('/notfound');
}])
Note that otherwise
wraps a rule
that returns the same url that receive.
You could check state change events in your app.run function.. something like this..
app.run(function ($rootScope, $state) {
$rootScope.$on("$stateNotFound", function (event, unfoundState, fromState, fromParams) {
//manage state not found
});
});