Guest User

Untitled

a guest
Jan 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /* In place redirect using location.replace()
  2. *
  3. * Call redirect.redirect() with same arguments as $location.update()
  4. * Call redirect.redirectHash() with same arguments as $location.updateHash()
  5. *
  6. * $route.otherwise({redirectTo: function() {
  7. * return redirect.redirectHash(...) }});
  8. *
  9. * Disuse after Vojta's $location replace is merged (0.10.1+).
  10. */
  11. angular.service('redirect', function() {
  12. var $safe_location = angular.injector(this.$new(),
  13. angular.extend({}, angular.service, {
  14. $browser: extend_browser(angular.service.$browser),
  15. }))('$location');
  16.  
  17. return {
  18. redirect: redirect,
  19. redirectHash: redirectHash,
  20. };
  21.  
  22. function redirect(href) {
  23. $safe_location.update(href);
  24. location.replace($safe_location.href);
  25. return $safe_location.href;
  26. }
  27.  
  28. function redirectHash(path, search) {
  29. $safe_location.updateHash(path, search);
  30. location.replace($safe_location.href);
  31. return $safe_location.hash;
  32. }
  33.  
  34. // Derive a $browser service where setUrl is noop, this makes $location safe
  35. // to use as a url calculator.
  36. function extend_browser(original) {
  37. extended.$inject = original.$inject.splice();
  38. function extended() {
  39. X.prototype = original.apply(this, arguments);
  40. function X() { this.setUrl = angular.noop; }
  41. return new X;
  42. }
  43. return extended;
  44. };
  45. }, {$inject: []});
Add Comment
Please, Sign In to add comment