5

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?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

8

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.

ashu
  • 1,019
  • 7
  • 9
  • why does this not work properly with ui-router? my routes are not working since I added that fix, although the otherwise is now working... – Ryan S Jul 19 '16 at 21:17