我已多次阅读这篇文章,并已按照那里的说明进行操作,但我无法使其正常工作。
AngularJS routing without the hash '#'
我有一个 Qt 应用程序,它向一个带有 # 的 URL 发送请求。该 URL 被路由到 Angular 代码。我想将其更改为不需要#。
这是我的 Angular 代码:
angular.module('app').config(function($routeProvider, $locationProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'home.html',
controller : 'home'
})
// route for the workitem page
.when('/workitem/:identifier', {
templateUrl : 'workitem.html',
controller : 'workitem'
});
$locationProvider.html5Mode(true);
});
这是我的 nginx 配置:
server {
listen 8000;
server_name foo.bar.com;
location / {
include /usr/local/nginx/conf/mime.types;
root /projects/larry/web;
}
}
在/projects/larry/web 中有一个 index.html,它使用 $routeProvider 加载 JS 模块。
当我转到 URL 时:http://foo.bar.com:8000/#/workitem/12345这很好用。 JS 已加载,$routeProvider 获取 URL 并执行它应该执行的操作。
但如果我省略 # 并转到 http://foo.bar.com:8000/workitem/12345那么JS代码没有加载,请求失败。
如何在没有哈希的情况下完成这项工作?
最佳答案
您还需要添加 <base href="/" />
在<head>
您的 html 页面。
示例如下。
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>My Website</title>
<base href="/" />
</head>
<body>
</body>
</html>
编辑
这会将您带到包含答案的 GitHub 帖子。 https://gist.github.com/cjus/b46a243ba610661a7efb
关于javascript - 没有哈希值 '#' 的 AngularJS 路由 - 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041074/