Using `history.replace` with Angular 1.x

时间:2017-04-06 17:09:18

标签: angular-routing html5-history angularjs-ng-route angularjs

I need help using history.replaceState on an Angular 1.x app.

My Chrome extension interacts with someone else’s app, and I want to load a Url in that app with an extra query parameter that my extension can read, and then use history.replaceState to remove from the url.

  • Step 1: open www.otherapp.com?extensionToken=MhtkWGUyS#/settings
  • Step 2: read xtensionToken=MhtkWGUyS
  • Step 3: replace/remove token with replaceState to www.otherapp.com#/settings

The trouble is that Angular 1.x doesn’t like history.replaceState to be used on its own: https://github.com/angular/angular.js/issues/3789

Angular seems to revert my update back to the original Url, or scramble the Url altogether. Does anyone have experience using history.replaceState with an Angular app?

One solution I thought of was to use Angular’s $location to make the state-change, but I’m don’t know how to access $location outside of the Angular scope. Does anyone know how I might access that?

The tricky part of generating a reference to $location is that my code will run with many different apps, with their own unique angular ng- elements, and I don’t know what those elements are on the page. I can assume that most have Angular at window.angular, but I can’t easily assume what the Angular app element will be for each page.

0 个答案:

没有答案