Kevin Meredith
Kevin Meredith

Reputation: 41919

ui-router's $urlRouterProvider.otherwise with HTML5 Mode

Consider the following slightly modified code from ui-router's wiki.

var myApp = angular.module('myApp',['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
    // for any unmatched url
    $urlRouterProvider.otherwise("/state1");

    $locationProvider.html5Mode(true);

    // now set up the states
    $stateProvider
        .state('state1', {
            url: '/state1',
            templateUrl: "partials/state1.html"
        })
        .state('state1.list', {
            url: "/list",
            templateUrl: "partials/state1.list.html",
            controller: function($scope) {
                $scope.items = ["A", "List", "of", "Items"];
            }
        })
        .state('state2', {
            url: "/state2",
            templateUrl: "partials/state2.list.html",
            controller: function($scope) {
                $scope.things = ["a", "set", "of", "things"];
            }
        })
});

Running python 3's SimpleHttpServer, I get a 404 error when trying to access: http://localhost:8000/state1234324324.

Why didn't $urlRouterProvider.otherwise("/state1"); re-direct all unknown routes to /state1, which per this question, has a defined state associated with the /state1 url?

Upvotes: 5

Views: 14045

Answers (1)

ashu
ashu

Reputation: 1039

This is because when you hit the server with urls like "/state123413", it will directly returns the 404 response (No routing at client side takes place).

What you need to do is to have a catchall route. e.g in express you can do

app.get('/*', function(req, res){
   res.render('defaulttemplate')   
}

This will force the routing on client side. Please see the ui router faq for common servers to set the catchall route.

Upvotes: 8

Related Questions