我原本认为Angular会重写出现在模板中锚标签的href属性中的URL,以使它们在html5模式或哈希模式下都能工作。然而,位置服务文档似乎表明HTML链接重写已经处理了哈希情况。因此,我期望在非HTML5模式下会插入哈希,在HTML5模式下则不会。
然而,似乎没有重写正在发生。下面的示例不允许我只更改模式。应用程序中的所有链接都需要手动重写(或者在运行时从变量派生)。我必须根据模式手动重新编写所有URL吗?
我在Angular 1.0.6、1.1.4或1.1.3中没有看到任何客户端URL重写。似乎所有href值都需要以#/开头,以供哈希模式使用,/用于html5模式。
是否需要某些配置才能进行重写?我是否误读了文档?还是做错了什么傻事?
这里有一个小例子:
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.js"></script>
</head>
<body>
<div ng-view></div>
<script>
angular.module('sample', [])
.config(
['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
//commenting out this line (switching to hashbang mode) breaks the app
//-- unless # is added to the templates
$locationProvider.html5Mode(true);
$routeProvider.when('/', {
template: 'this is home. go to <a href="/about"/>about</a>'
});
$routeProvider.when('/about', {
template: 'this is about. go to <a href="/"/>home</a'
});
}
])
.run();
</script>
</body>
附言:重新阅读我的问题后,我发现我在使用“重写”一词时并没有特别明确地指出谁何时需要进行重写。这个问题是关于如何让Angular在呈现路径时重写URL并如何使其统一解释JS代码中的路径,而不是关于如何使Web服务器对请求进行HTML5兼容的重写。