【发布时间】:2018-10-06 23:54:20
【问题描述】:
我正在用 angularjs 和 PHP 开发一个博客。使用 nodejs 一切正常,我可以使用“stateProvider”来路由应用程序。现在我正在使用 xampp,但是当我在 url 中插入“localhost / home / content”类型的地址时告诉我“找不到对象”,就好像实际上要在服务器上查找对象而不是使用路由功能一样angularjs的。相反,如果我插入“localhost / index_2.html”,它实际上会正确显示页面,并且 url 变为“localhost / home / content”(正如我所期望的那样)。我该如何解决?谢谢!
app.js
var importPath = "/partial/";
var home = importPath+"home/";
var homeContainer = home + 'homeContainer.html';
var homeContent = home + 'homeContent.html';
app.config(
["$stateProvider", "$urlRouterProvider", "$locationProvider",
function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider
.state('home', {
templateUrl: homeContainer,
})
.state('home.homeContent', {
url: '/home/content',
templateUrl: homeContent,
});
$urlRouterProvider.otherwise("/home/content");
$locationProvider.html5Mode(true);
}
]);
app.run(['$rootScope', '$location',
function ($rootScope, $location)
{
console.log("location ", $location.path());
}
]);
index_2.html
......
<body>
...
<div ui-view></div>
...
</body>
【问题讨论】:
-
可能是您的
html5Mode在没有哈希爆炸的情况下中断了路由 (#!) -
这与 xampp 或 nodejs 无关。这是关于你使用
$locationProvider.html5Mode(true) -
我不应该使用html5模式吗?那么html5模式的目的是什么?谢谢
-
它删除了 hash-bang,将 URL 从
www.website.com/#!/some/other/path转换为www.website.com/some/other/path。看起来 专业,但它会破坏您的路由,因此您必须手动修复它(通常在.htaccess中)。如果你可以避免它,那么就不要使用它。删除它后,确保您的链接正常工作:href="/link"应该是href="#!/link"等(ui.router只需使用状态:ui-sref="state") -
谢谢大家,我删除了 html5 模式,现在它可以正常工作了。
标签: php angularjs node.js routing xampp