javascript - 没有哈希值 '#' 的 AngularJS 路由 - 无法正常工作

标签 javascript angularjs nginx

我已多次阅读这篇文章,并已按照那里的说明进行操作,但我无法使其正常工作。

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/

相关文章:

javascript - 如何测试输入表单域是否获得焦点?

javascript - 为什么不内置类似框架的功能?

javascript - 在 IE11 中通过 css() 缓慢进行 jQuery 导航重新定位

javascript - 如何获取没有元素id的元素?

AngularJS:带有键值的 ng-repeat - 更新对象

javascript - AngularJS 和效率(或 : storing values for multiple uses)

php - Docker php 设置安装 pdo 扩展

python - "no python application found"uWSGI + nginx + Ubuntu 13

javascript - 我怎样才能让 AngularJS 绑定(bind)与花式框对话框一起工作?

nginx - 在向上游发送请求之前,先从API获取信息