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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With