当通过 AngularJS 启用 html5Mode 时$locationProvider.html5Mode(true)
,当您到达网站较深处的页面时,导航似乎会倾斜。
例如:
http://www.site.com
当我导航到根目录时,我可以单击站点中的所有链接,Angular 的$routeProvider
将接管网站导航并加载正确的视图。
http://www.site.com/news/archive
但是当我导航到这个网址时(或者当我在像上面这样的深层链接上时点击刷新......)这个导航无法按照我的预期工作。
首先,我们作为$locationProvider.html5Mode 的文档指定,我们捕获服务器上的所有 url,类似于otherwise
以角度路由,并返回与根域相同的 html。但如果我随后检查$location
对象从内部run
角度的函数,它告诉我http://www.site.com
is my host
然后/archive
is my path
. the $routeProvider
到达在.otherwise()
条款,因为我只有/news/archive
作为有效路线。该应用程序做了一些奇怪的事情。
也许服务器上的重写需要以不同的方式完成,或者我需要以角度指定内容,但目前我不知道为什么角度看到的路径没有/news
包括部分。
示例 main.js:
// Create an application module
var App = angular.module('App', []);
App.config(['$routeProvider', '$locationProvider', function AppConfig($routeProvider, $locationProvider) {
$routeProvider
.when(
'/', {
redirectTo: '/home'
})
.when('/home', {
templateUrl: 'templates/home.html'
})
.when('/login', {
templateUrl: 'templates/login.html'
})
.when('/news', {
templateUrl: 'templates/news.html'
})
.when('/news/archive', {
templateUrl: 'templates/newsarchive.html'
})
// removed other routes ... *snip
.otherwise({
redirectTo: '/home'
}
);
// enable html5Mode for pushstate ('#'-less URLs)
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
}]);
// Initialize the application
App.run(['$location', function AppRun($location) {
debugger; // -->> here i debug the $location object to see what angular see's as URL
}]);
Edit根据请求,服务器端的更多详细信息:
服务器端由 zend 框架的路由组织,它处理自己的路由(用于在特定的情况下向前端提供数据)/api
urls,最后,如果没有绑定特定的其他路由,则有一个包罗万象的路由,它提供与根路由相同的html。
所以它基本上在深度链接的路线上提供主页 html。
Update 2研究问题后,我们注意到该路由在 Angular 1.0.7 stable 上运行良好,但在 Angular 1.1.5不稳定版中显示了上述行为。
我已经检查了更改日志,但到目前为止还没有发现任何有用的东西,我想我们可以将其作为错误提交,或者将其作为与他们所做的某个更改相关的不需要的行为提交,或者只是等待,看看它是否得到修复稍后将发布稳定版本。