当我从索引访问我的页面并开始浏览时一切正常,但是当我在 /
以外的路线上时例如在 /details/123
我刷新页面(我配置了 URL 重写)路由设置不正确。
这意味着,当我从索引正常浏览时检查位置路径时,我在 /details/123
位置路径是 /details/123
正如预期的那样,但是当我刷新页面时,我仍然在 /details/123
位置路径更改为 /123
导致 ngView 显示错误的 View 。
我正在使用 html5 模式和 Angular v.1.1.5
更新:我创建了一个简单的例子 here来说明问题。
我没有任何特殊设置,我认为不是服务器问题。我在 python 中的另一个应用程序中遇到了同样的问题,其中重定向是在应用程序内部完成的。.htaccess
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php
</IfModule>
最佳答案
这可能与 Angular 1.1.5 期间出现的一个令人讨厌的问题有关,并且是核心库中的一个错误。对许多人有效的解决方案是将以下标签添加到您的 index.html 头部。
如果您的应用程序在域的根目录下运行。
<head>
...
<base href="/"></base>
...
</head>
或者,如果您的应用程序在子目录中运行,请指定该子目录(例如“myapp”):
<head>
...
<base href="/myapp/"></base>
...
</head>
此外,您还可以尝试一组新的重写规则。此配置适用于许多 ui-router 用户。
<IfModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</IfModule>
关于angularjs - 浏览器刷新时错误的基本路径,ngView 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867922/