@greg.kindel(已接受的解决方案)的解决方法对我不起作用。它抛出了很多关于无限摘要循环的错误。我正在使用 Angular 1.5.8。
我能够将该解决方法调整为以下内容以使事情正常进行。我希望它可以避免其他人的悲伤。
angular.module('sample', [])
.config(['$provide', function ($provide) {
$provide.decorator('$browser', ['$delegate', '$window', function ($delegate, $window) {
$delegate.onUrlChange = function () {};
//
// HACK to stop Angular routing from manipulating the URL
//
// The url() function seems to get used in two different modes.
//
// Mode 1 - Zero arguments
// There are no arguments given, in which case it appears that the caller is expected the
// browser's current URL (a string response).
//
// Mode 2 - Three arguments
// It receives three arguments (url, some_boolean, null). It seems the caller is expecting
// the browser's URL to be updated to the given value. The result of this function call is
// expected to be the $delegate itself, which will subsequently get called with no arguments
// to check the browser's URL.
//
// The Hack:
// We save the URL so that we can lie to the caller that the URL has been updated to what was
// requested, but in reality, we'll do nothing to alter the URL.
//
var savedUrl = null
$delegate.url = function (url, ...args) {
if (!!url) {
savedUrl = url;
return $delegate;
} else {
return !!savedUrl ? savedUrl : $window.location.href;
}
};
return $delegate;
}]);
}])