【问题标题】:AngularJS with RequireJS routing removes hashAngularJS 与 RequireJS 路由删除哈希
【发布时间】:2014-11-15 03:25:39
【问题描述】:

我正在使用 Angular Routes 来确定要显示给用户的视图,如下所示:

define(['angular', './app'], function(angular, app) {
    'use strict';
    return angular.module('app', ['ngRoute']).
    config(function($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl:'assets/partials/partial1.html',
                controller: 'MyCtrl1'
            }).when('/mada', {
                templateUrl:'assets/partials/partial2.html',
                controller: 'MyCtrl2'
            })
            .otherwise({redirectTo: '/'});

    });

});

目前,路由工作,但只是这样:localhost/#/mada 或 localhost/#/ 我希望能够看到 localhost/mada

我知道添加

$locationProvider.html5Mode(true);

将摆脱散列。但这仍然无法让用户访问:localhost/mada,因为这会返回服务器 404。

这可以实现吗?怎么样?

【问题讨论】:

    标签: javascript angularjs routes requirejs http-status-code-404


    【解决方案1】:

    如果您不使用服务器端语言,则将此代码放在 html 头中。

    <base href="/">
    

    喜欢:

    <head>
        <base href="/">
    </head>
    

    并将其放入配置块中:

    $locationProvider.html5Mode(true);
    

    如果您使用的是服务器端语言,请使用以下代码:

    app.get('*', function(req, res) {
        res.render('index');
    });
    

    而不是

    app.get('/', function(req, res) {
        res.render('index');
    });
    

    并将此代码放入配置块中:

    $locationProvider.html5Mode(true);
    

    【讨论】:

      猜你喜欢
      • 2016-08-22
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2018-04-13
      相关资源
      最近更新 更多