Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 796
Kevin Meredith Avatar asked Jun 06 '14 17:06

Kevin Meredith


People also ask

What is HTML5 mode in the Ui router framework?

The UI Router framework gives you ultimate control over the URLs generated for your site by allowing you to enable HTML5 mode. When enabled, this mode does not generate hash (#) locations, but uses the HTML5 history API to generate clean URLs.

What is the Ui router framework?

Beyond rendering HTML content, the UI Router framework supports URL routing, the ability to resolve dependencies before controllers are initialized, named and nested views, utility filters, state change events, and declarative transitions among states. There are a few different ways you can use to move between different states.

What is the use of $urlrouterprovider?

The $urlRouterProvider is an object that gives you control over how the browser's location is managed and observed. In the context of the UI Router, $urlRouterProvider is used to help define a catch-all navigation scenario.

How does the Ui router framework use anchors?

As the UI Router framework evaluates this directive, the anchor is transformed to have the appropriate URL value. For example: Notice that the element is updated to include an href attribute with a value corresponding to how the URL must be updated to navigate to the About Us page.


1 Answers

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.

like image 58
ashu Avatar answered Sep 19 '22 17:09

ashu