javascript - AngularJS SPA 路由获取 URL 为 "/#!"而不是 "#"

标签 javascript html angularjs mean-stack hashbang

在我的 SPA AngularJS 应用程序中,我得到的 URL 为 http://localhost:4200/#!/而不仅仅是# (hash bang) 。因此,路由似乎无法以通常的方式工作。 我确实经历过这个 stackoverflow Question但没有找到任何解决方案。有人知道摆脱这个额外感叹号的解决方案吗?

编辑: 在我的index.ejs: 我有<a href="#about">about</a>

在我的approutapp.js: 我有

var myapp = angular.module("myApp", ["ngRoute"]);
myapp.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "/static/list.ejs"
    })
    .when("/about", {
        templateUrl : "/static/about-us.ejs"
    })

});

myapp.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);

但是我仍然得到的 URL:http://localhost:4200/#/!/

然后前往http://localhost:4200/about 页面挂起

最佳答案

尝试使用$locationProvider.hashPrefix('')

原因是:

如果浏览器是HTML5浏览器angularJS会将其重定向到#!

否则就只是#

阅读 $location 上的此文档了解更多信息:

Opening a regular URL in a legacy browser -> redirects to a hashbang

URL Opening hashbang URL in a modern browser -> rewrites to a regular URL 

HTML5 mode

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents. If the HTML5 History API is not supported by a browser, the $location service will fall back to using the hashbang URLs automatically. This frees you from having to worry about whether the browser displaying your app supports the history API or not; the $location service transparently uses the best available option.

Opening a regular URL in a legacy browser -> redirects to a hashbang URL Opening hashbang URL in a modern browser -> rewrites to a regular URL Note that in this mode, AngularJS intercepts all links (subject to the "Html link rewriting" rules below) and updates the url in a way that never performs a full page reload.

关于javascript - AngularJS SPA 路由获取 URL 为 "/#!"而不是 "#",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480753/

相关文章:

javascript - 寻找在不创建其他 div 的情况下以每 100 速率随机显示 div 的解决方案

html - ng-app 与 data-ng-app,有什么区别?

javascript - 如何删除父标签名称?

javascript - 映射到数组后创建动态对象键

javascript - ng-click 在响应式 Angular 数据表中不起作用

javascript - 使用 AngularJS 选择 DOM 元素

angularjs - 将参数传递给具有隔离范围的嵌套指令

javascript - 无法设置子对象的未定义属性

javascript - 堆叠 HTML 元素的顺序

html - CSS 动画 - 悬停时平移到某个点