避免使用ng路由对无散列url进行路由

avoid routing of hash-less urls with ng-route

本文关键字:路由 url ng      更新时间:2024-04-15

比方说,我正在浏览localhost/index.html#/view1,并且我有一个指向localhost/index.html的链接。当我点击它时,angular会通过它的ng路由模块对它进行处理,它会落入我的重定向子句中。

这是路由配置的摘录:

$routeProvider
    .when( "/view1", route1 )
    .otherwise({redirectTo:'/view1'}); // localhost/index.html clicks fall here

我可以使用ng点击,然后用location.href重定向javascript,但这不是一个理想的解决方案,因为我会丢失href链接(SEO、书签、选项卡)。

tl;dr

如何告诉angularJs ng route不要处理没有哈希的URL

编辑:我不使用html5模式,也不打算使用它。

如果您正在询问忽略url链接的角度路由服务。参见$location 文档

在以下情况下,链接不会被重写;相反浏览器将执行一个完整的页面重新加载到原始链接。

包含目标元素的链接

Example: <a href="/ext/link?a=b" target="_self">link</a>

到不同域的绝对链接

Example: <a href="http://angularjs.org/">link</a>

以"/"开头的链接,在定义基础时导致不同的基础路径

Example: <a href="/not-my-base/link">link</a>