AngularJS:如何从 URL 中删除 #!/(bang 前缀)? [英] AngularJS: How to remove #!/ (bang prefix) from URL?

查看:30
本文介绍了AngularJS:如何从 URL 中删除 #!/(bang 前缀)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了 $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(''); 但没有区别.另外让我告诉你,我在地址栏中输入地址并按回车键.我做得对吗?浏览器如何发现不需要从服务器刷新?

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.comhttp://example.com/#!/list-rolehttp://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 模式并删除模块配置中的前缀 !

$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('');

2) 然后在 index.html

<head>
    ...
    <base href="/">
</head>

这篇关于AngularJS:如何从 URL 中删除 #!/(bang 前缀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆