Unknown User Unknown User - 1 year ago 71
AngularJS Question

Page doesn't redirect to the specified page on invalid url

I have the below Angular function.

Function :

var App = angular.module('app', ['ui.bootstrap', 'ngRoute', 'ngAnimate']);

App.config(['$routeProvider', function($routeProvider) {


.when('/', {
templateUrl: 'views/dashboard.html'
.when('/:pages', {
templateUrl: function(routeParams) {
return 'views/' + routeParams.pages + '.html';
.otherwise({redirectTo: '404.html'})


I have a sidebar navigation control. And I've created 4 pages.

So when I click on those navigation items the respective pages open up correctly.

And there are some more pages that I haven't created yet. But as per the below function.

When I don't have something that doesn't exists, it must return to
file that exists in the root of the folder.

What's happening is, I don't get a error in the console and the url the in the address bar reflects the last clicked valid page.

Someone let me know where I'm doing the mistake and whether this method of is correct for dynamic routing?

Answer Source

The otherwise() part catches paths that do not match to any of the specified routes.
In your case, the route matches, but the template is not available at the specified URL.
$routeProvider doesn't know anything about it and can't do much either.

What you can do, is somehow (see below) check if the template is available and if it is not use $location to redirect to an appropriate path (e.g. /error/404).

In order to determine if the page is valid (i.e. its template is available) you could try to access the template (using $http) and catch any errors (indicating there is no template) etc. But I don't like this approach (as relying on a the availability of a template for determining the page's existence/validity isn't a very good practise imo - e.g. it could easily lead to misleading error messages in case of a network or server problem etc).

My favorite approach is to keep a list of "valid"/existing pages and check against it. If the current page should be available, proceed as usual to fetch the template etc. If not, redirect to an error page.

The logic described above could be placed in $routeProvider's resolve property (so it gets executed before the controller is instantiated and the view loaded).


var App = angular.module(...);

// This should be a constant, so it can
// get injected into configuration blocks
App.constant('EXISTING_PAGES', [

app.config(function ($routeProvider, EXISTING_PAGES) {    
        when('/', {
            templateUrl: 'views/dashboard.html'
        when('/:page', {
            templateUrl: function (routeParams) {
                return 'views/' + routeParams.page + '.html';
            resolve: {
                exists: function ($location, $route) {
                    // Because this is executed before the instantiation of the 
                    // controller and the view is not fully loaded yet, the parameters
                    // should be accessed via `$route.current.params`
                    if (EXISTING_PAGES.indexOf($route.current.params.page) === -1) {
                        // This is not a valid/eisting page, 
                        let's redirect to an appropriate error page
                    return true;
        // This should be a separate route, so we can redirect to it
        when('/error/404', {
            templateUrl: '404.html'
            redirectTo: '/error/404'

See, also, this short demo.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download