AngularJS路由没有#“#” [英] AngularJS routing without the hash '#'
问题描述
我学习AngularJS并有一件事真的让我很烦。
I'm learning AngularJS and there's one thing that really annoys me.
我用 $ routeProvider
来声明路由规则为我的应用程序:
I use $routeProvider
to declare routing rules for my application:
$routeProvider.when('/test', {
controller: TestCtrl,
templateUrl: 'views/test.html'
})
.otherwise({ redirectTo: '/test' });
但是当我浏览到我的应用程序在浏览器中我看到应用程序/#/测试
而不是应用程序/测试
所以我的问题是,为什么AngularJS添加此哈希#
来的网址?是否有可能避免呢?
So my question is why AngularJS adds this hash #
to urls? Is there any possibility to avoid it?
推荐答案
在事实上,你需要对非HTML5浏览器的#(包括hashtag)。
In fact you need the # (hashtag) for non HTML5 browsers.
否则会只是做一个HTTP调用服务器在提到HREF。
#是一个古老的浏览器短路不火的要求,这使得许多JS框架,以建立自己的客户方重新路由在此之上。
Otherwise they will just do an HTTP call to the server at the mentioned href. The # is an old browser shortcircuit which doesn't fire the request, which allows many js frameworks to build their own clientside rerouting on top of that.
您可以使用 $ locationProvider.html5Mode(真)
来告诉角(如果可用)使用HTML5策略。
You can use $locationProvider.html5Mode(true)
to tell angular to use HTML5 strategy if available.
下面是支持HTML5的浏览器战略的列表: http://caniuse.com/#feat=history
Here the list of browser that support HTML5 strategy: http://caniuse.com/#feat=history
这篇关于AngularJS路由没有#“#”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!