Javascript 在Angular中启用HTML5模式

Javascript 在Angular中启用HTML5模式,javascript,angularjs,Javascript,Angularjs,我正在用AngularJS编写我的第一个SPA,但遇到了路由问题 我正在尝试启用HTML 5,如下所示: .config(function ($routeProvider, $locationProvider) { $locationProvider.html5Mode(true); $routeProvider.otherwise({ templateUrl: "/angular/components/booking-system/booking-system

我正在用AngularJS编写我的第一个SPA,但遇到了路由问题

我正在尝试启用HTML 5,如下所示:

.config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider.otherwise({
        templateUrl: "/angular/components/booking-system/booking-system-template.html"
    });
})
但我知道需要对服务器(IIS)进行一些配置。有人能告诉我需要做什么吗?关于这个主题的各种帖子有点不清楚


谢谢,M

我认为您不需要配置服务器以使HTML5模式正常工作。你唯一需要做的就是添加

<base href="/">

到您的

请小心,因为此配置仅在您正在运行的应用位于服务器的根目录(例如“”)时适用

如果希望应用程序在子上下文(例如“.”)中运行,则需要按以下方式指定基本href:

我希望这能解决你的问题


Lukas

如果您希望启用HTML5模式,则需要在服务器端进行更改,以便在任何URL请求中返回入口点。然后angular应用程序将解析URL并加载所需页面

请对此进行详细说明,以供参考: 如何:配置服务器以使用HTML5模式

如果您使用的是
ng view
,那么您应该只需要IIS中的一条规则就可以实现此功能

首先,你需要一个新的

然后,您需要设置一个规则,将所有路由重定向回入口点,以便angular正确地拾取它

在重写规则中,您希望看到如下内容:

.config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider.otherwise({
        templateUrl: "/angular/components/booking-system/booking-system-template.html"
    });
})

您也可以使用以下命令直接编辑web.config:

<rule name="Angular Rewrite" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/" />
</rule>

您还需要确保在
html


不幸的是,它没有。我正在安装404。在上面添加了更多关于我路线的详细信息。^试着看一下我确实看了卢卡斯的帖子,但没有帮助