简体   繁体   English

ui-router的$ urlRouterProvider.otherwise与HTML5模式

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

Consider the following slightly modified code from ui-router's wiki. 请考虑ui-router的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 . 运行python 3的SimpleHttpServer,尝试访问时遇到404 errorhttp://localhost:8000/state1234324324

Why didn't $urlRouterProvider.otherwise("/state1"); 为什么没有$urlRouterProvider.otherwise("/state1"); re-direct all unknown routes to /state1 , which per this question , has a defined state associated with the /state1 url? 将所有未知路由/state1/state1 ,根据这个问题 ,它具有与/state1 url相关联的已定义state

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). 这是因为当您使用“/ state123413”等网址访问服务器时,它将直接返回404响应(客户端没有路由发生)。

What you need to do is to have a catchall route. 你需要做的是拥有一条捕获路线。 eg 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. 有关常用服务器的信息,请参阅ui router faq以设置catchall路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM