AngularJS routing hijacking non base URLs when page reload is expected

柔情痞子 提交于 2019-12-04 03:08:18

Looking at the code, it looks important that the base is "/blog/" rather than "/blog". Once I changed that I was able to refer to links relatively:

  <a href="./Book/Moby">Moby</a> |
  <a href="./Book/Gatsby">Gatsby</a> |
  <a href=".">base root</a> |
  <a href="/Outside">Outside</a> |  // gets a nice 404

My jsfiddle, note that location.pathname is always setting "[path]/", this also works hardcoded /_display/ while editing, but jsfiddle switches between _display/ and workingdir/show/ when running edited and save fiddles.

Chandermani

As per Angular documentation

In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.

  • Links that contain target element
    Example: <a href="/ext/link?a=b" target="_self">link</a>
  • Absolute links that go to a different domain
    Example: <a href="http://angularjs.org/">link</a>
  • Links starting with '/' that lead to a different base path when base is defined
    Example: <a href="/not-my-base/link">link</a>

See if this helps you

You could also do something like this:

var redirect = function(skip, url) {
    console.log("Redirecting to ", url);
    window.location.href = url
};
$routeProvider
    .when('/inside-router', { templateUrl: 'something.html' })
    .when('/get-me-away.html', { redirectTo: redirect })
    .when('/static/:file', { redirectTo: redirect })

Here is a JSFiddle with a working example.

Basically you're abusing that AngularJS will call a function given in the redirectTo property and then do nothing more if the function doesn't return a string.

hope it helps:

$routeProvider
.when('...', {})
.otherwise({
  redirectTo: function () {
    window.location = location.pathname;
}});

using:
1.3.10/angular.js
1.3.10/angular-route.js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!