AngularJS:如何从URL中删除#!/(爆炸前缀)? [英] AngularJS: How to remove #!/ (bang prefix) from URL?
问题描述
我已经完成了 $ locationProvider.html5Mode(true);
,但是它不起作用。每当我访问 http://example.com
时,它都会转到 http://example.com/#!/
。代码在这里给出:
I already have done $locationProvider.html5Mode(true);
but it is not working. Whenever I access http://example.com
it goes to http://example.com/#!/
. Code is given here:
var myApp = angular.module('myApp', ['ui.router', 'ui.bootstrap']);
myApp.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
myApp.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
// For any unmatched url, redirect to EXTRANET home page
$urlRouterProvider.otherwise('/');
// Now set up the states
$stateProvider
.state('listrole', {
url: "/list-role",
views: {
'main-content': {
templateUrl: rootUrl+ 'views/main.html',
controller: 'rolesCtrl'
}
}
})
$locationProvider.hashPrefix('');
$locationProvider.html5Mode(true);
});
更新
我添加了 $ locationProvider.hashPrefix('');
也没有区别。还让我告诉您,我正在地址栏中输入地址,然后按Enter。我做对了吗?浏览器将如何发现它不需要从服务器刷新?
Update
I added $locationProvider.hashPrefix('');
as well but no difference. Also let me tell you that I am enter address in address bar and hitting enter. Am I doing right? how will browser find that it does not need to refresh from server?
更新#2
理想情况下,我希望URL为 http://example.com
和 http://example.com/#!/list-role
到 http://example.com/list-role
。现在 http://example.com/list-role
给出404
Ideally I want URL as http://example.com
and http://example.com/#!/list-role
to http://example.com/list-role
. Right now http://example.com/list-role
gives 404
更新#3
如果有帮助,我正在使用nginx代理。
I am using nginx proxy, if that helps.
更新#4
已擦除缓存。现在,我可以看到 http://example.com
而不是 http://example.com/#!
仍然 http://example.com/list-role
给出404
Erased Cache. Now I can see http://example.com
instead of http://example.com/#!
but still http://example.com/list-role
gives 404
推荐答案
如果要删除此前缀,请将此代码添加到配置中:
If you want to remove this prefix, add this code to your config:
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
在此处了解更多信息。
如果要删除整个前缀(#
,而不仅仅是!
),您可以尝试此解决方案:
If you want to remove the whole prefix (#
and not only !
), you may try this solution:
1)激活HTML5模式并在模块配置中删除前缀!
1) Activate the HTML5 mode and remove the prefix !
in your module config
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('');
2),然后将基数设置为 / $ c index.html上
< head>
中的$ c>
2) And then set base to /
in the <head>
on your index.html
<head>
...
<base href="/">
</head>
这篇关于AngularJS:如何从URL中删除#!/(爆炸前缀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!