gpt4 book ai didi

AngularJS:如何通过深度链接启用 $locationProvider.html5Mode

转载 作者:行者123 更新时间:2023-12-03 05:30:41 24 4
gpt4 key购买 nike

通过 $locationProvider.html5Mode(true) 在 AngularJS 中启用 html5Mode 时,当您登陆网站更深的页面时,导航似乎会倾斜。

例如:

  • http://www.site.com
    当我导航到根目录时,我可以单击网站中的所有链接,Angular 的 $routeProvider 将接管网站导航并加载正确的 View 。

  • http://www.site.com/news/archive
    但是当我导航到这个网址时(或者当我在像上面这样的深层链接上时点击刷新......)这个导航无法按照我的预期工作。首先,我们的作为Documentation for $locationProvider.html5Mode指定,我们捕获服务器上的所有 url,类似于 Angular 中的 otherwise 路由,并返回与根域相同的 html。但是,如果我然后从 Angular 的 run 函数中检查 $location 对象,它会告诉我 http://www.site.com 是我的主机/archive 是我的路径$routeProvider 到达 .otherwise() 子句,因为我只有 /news/archive 作为有效路由。该应用程序做了一些奇怪的事情。

也许服务器上的重写需要以不同的方式完成,或者我需要以 Angular 指定内容,但目前我不知道为什么 Angular 看到的路径不包含 /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
}]);

编辑根据请求,服务器端的更多详细信息:服务器端由 zend 框架的路由组织,它处理自己的路由(用于在特定的 /api url 上向前端提供数据,最后有一个包罗万象的路由如果没有绑定(bind)特定的其他路由,它将提供与根路由相同的 html。所以它基本上在该深层链接路径上提供主页 html。

更新2研究问题后,我们注意到该路由在 Angular 1.0.7 stable 上运行良好,但在 Angular 1.1.5不稳定版中显示了上述行为。

我已经检查了更改日志,但到目前为止还没有发现任何有用的东西,我想我们可以将其作为错误提交,或者将其作为与他们所做的某些更改相关的不需要的行为提交,或者只是等待看看是否会发生get 已在后续发布的稳定版本中修复。

最佳答案

发现没有错误。只需添加:

<base href="/" />

给您的<head /> .

关于AngularJS:如何通过深度链接启用 $locationProvider.html5Mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18214835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com