Angular.js路由抛出404而没有# [英] Angularjs routing throws 404 without #
问题描述
我正在使用AngularJS创建一个网站,并且正在使用ngRoute模块来处理页面请求.
I am creating a website with AngularJS and I am using the ngRoute module to handle page requests.
在我的app.config
函数中,我设置了如下路由:
In my app.config
function I have setup the routing as follows:
app.config(function($routeProvider, $locationProvider, $httpProvider){
$locationProvider.html5Mode(true);
$routeProvider.when('/', {
templateUrl: 'pages/index.html',
controller: 'mainController'
}).when('/testpage', {
templateUrl: 'pages/test.html',
controller: 'testController'
}).otherwise({
redirectTo: '/'
});
});
当我尝试访问测试页面时,请在MAMP本地服务器上,在页面名称之前添加#
,如下所示:http://dev.mysite.com:8888/#testpage
一切正常.
When I try to access the test page, on my MAMP local server, by adding a #
before the page name like this: http://dev.mysite.com:8888/#testpage
everything works fine.
但是,如果我没有在页面名称前添加#
,例如:http://dev.mysite.com:8888/testpage
我会收到404错误.
However, if I don't add the #
before the page name, like this: http://dev.mysite.com:8888/testpage
I get a 404 error.
我已经在包含<div ng-view>
标记的index.html页面的<head>
中添加了<base href="/">
.
I have added <base href="/">
to the <head>
of my index.html page that contains the <div ng-view>
tag.
如果有人可以解释我在做什么错,我将非常感激.
I would very much appreciate it if someone could explain what I am doing wrong.
推荐答案
Angular寻找#符号来处理路由.路由功能会解释#符号后的所有内容.没有#符号,没有角线.如果可以在Html5模式下运行,则可以将其关闭,但是要在较旧的浏览器上运行,则需要它.
Angular looks for the # symbol to handle routing. Everything after the # symbol is interpreted by the routing function. No # symbol, no angular route. If you can run in Html5 mode, then you can turn this off, but to work with older browsers, it is needed.
这篇关于Angular.js路由抛出404而没有#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!