【问题标题】:ui-router's $urlRouterProvider.otherwise with HTML5 Modeui-router 的 $urlRouterProvider.otherwise 与 HTML5 模式
【发布时间】:2014-06-07 02:02:36
【问题描述】:

考虑以下来自 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"];
            }
        })
});

运行 python 3 的 SimpleHttpServer,我在尝试访问时得到一个 404 error:http://localhost:8000/state1234324324。

为什么$urlRouterProvider.otherwise("/state1"); 不将所有未知路由重定向到/state1,因为question 有一个与/state1 url 关联的已定义state?

【问题讨论】:

    标签: html angularjs angular-ui-router


    【解决方案1】:

    这是因为当你用“/state123413”之类的url访问服务器时,它会直接返回404响应(客户端没有路由)。

    你需要做的是有一个包罗万象的路线。例如在快递中你可以做

    app.get('/*', function(req, res){
       res.render('defaulttemplate')   
    }
    

    这将强制在客户端进行路由。通用服务器请查看ui路由器faq设置包罗万象的路由。

    【讨论】:

    • 为什么这不能与 ui-router 一起正常工作?自从我添加了该修复程序以来,我的路线无法正常工作,尽管其他方式现在正在工作......
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 2014-09-05
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2018-10-21
    相关资源
    最近更新 更多