13

AngularJS 1.2.0 (but lower versions have the same problem)

I have a web app with some widgets and want to save their state into URL. I do this now with $location.hash('param1=1&param2=678') command. But I get url like: domain.com/##param1=1&param2=678 It works ok, I can restore state of my app. One problem I have with that is when someone click such a link in, for example, mail app, their browser encodes one of hashes with /23 and so my app goes wrong. How can i solve this? Thanks

Here is plunk: http://plnkr.co/edit/VVjEUzROou6hu8B8sURa?p=preview You need open it in new window to able to test hashes

outluch
  • 842
  • 7
  • 15
  • maybe because href="#" is in your anchor tag? try with href="javascript:" instead. – Magne Apr 16 '14 at 09:52
  • I just noticed that same behaviour (double hash) is happening in the [official docs](https://docs.angularjs.org/api/ng/service/$anchorScroll), weird – piotr_cz Dec 04 '14 at 12:34

3 Answers3

8

for standard hash locations in angular, you use

location.path("myappstate/1");

as angular is set up to think of first hash state as the primary url or path for the app. The location.hash() setups a secondary hash on the primary hash state (path)

chrismarx
  • 9,717
  • 8
  • 77
  • 92
1

This is not the best answer, but you could try to inject $locationProvider and set:

$locationProvider.html5Mode(true);

That way angular runs in HTML5 mode and it does not use hashbangs. I recommend this SO question for more info.

Community
  • 1
  • 1
Capaj
  • 3,577
  • 2
  • 39
  • 50
0

Try using

$location.search("key", "value")

instead. If you have complex state you can even directly supply an object to the search method:

$location.search({foo: bar, baz: duh});
Thomas
  • 9,560
  • 2
  • 19
  • 33
  • I used .search() for serialization before, but gone to hash, because it is better to parse strings myself. I still didnt solve the problem. If someone want to see in in action: http://infolek.ru – outluch Feb 05 '14 at 18:07