UI的路由器的$ urlRouterProvider.otherwise与HTML5模式 [英] ui-router's $urlRouterProvider.otherwise with HTML5 Mode

查看:1448
本文介绍了UI的路由器的$ urlRouterProvider.otherwise与HTML5模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下略作修改$ C $从UI的路由器的维基℃。

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"];
            }
        })
});

执行Python 3的SimpleHttpServer,我得到一个 404错误试图访问时的http://本地主机:8000 / state1234324324

为什么没有 $ urlRouterProvider.otherwise(/ STATE1); 重新定向所有未知的路线 / STATE1 ,其中每本问题,都有一个定义状态与<$ C $关联C> / STATE1 URL?

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?

推荐答案

这是因为当你点击服务器包含/ state123413的网址,就会直接返回404响应(无路由在客户端发生)。

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).

您需要做的是有一个包罗万象的路线。例如在EX preSS你可以做

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')   
}

这将迫使客户端上的路由。请参阅UI路由器<一href=\"https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode\">faq为共同的服务器来设置全部接收路由。

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

这篇关于UI的路由器的$ urlRouterProvider.otherwise与HTML5模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆