没有散列“#"的 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' });
但是当我在浏览器中导航到我的应用程序时,我看到的是 app/#/test
而不是 app/test
.
but when I navigate to my app in browser I see app/#/test
instead of app/test
.
所以我的问题是为什么 AngularJS 将这个哈希 #
添加到 urls 中?有没有可能避免它?
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.
否则他们只会在提到的 href 处对服务器进行 HTTP 调用.# 是一个旧的浏览器短路,它不会触发请求,它允许许多 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(true)
告诉 angular 使用 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屋!