Turn off URL manipulation in AngularJS

前端 未结 5 1012
旧时难觅i
旧时难觅i 2020-11-27 04:07

I\'m trying to write my first web-app with Angular.

In the normal mode (html5Mode off), Angular forces the address\'s hash part to look like a \"path\" (adding a lea

5条回答
  •  情深已故
    2020-11-27 04:39

    The workaround from @greg.kindel (the accepted solution) didn't work for me. It threw lots of errors about an infinite digest loop. I'm using Angular 1.5.8.

    I was able to adjust that workaround to the following to get things working. I hope it saves someone else grief.

    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;
        }]);
      }])
    

提交回复
热议问题