0

I have sort of a strange question, I'm just trying to see if this is possible.

What I am looking for is a way to update the url as the user is using the app - when they click a few different items on the page I would like it to almost "bind" to the urlParameters I have set up. So I have like -

 url: 'app/:mod1/:mod2',

in my route and as the user interacts with buttons on the page, 2 of them are binded to scopes in the controllers, lets say

 $scope.mod1 = true;
 $scope.mod2 = false;

And then as the user interacts with them it would change the url accordingly. So if they clicked the buttons and made mod1 = false and mod2 = true, the url would change to

/app/false/true

Here is the catch - I would love to be able to do this without refreshing the page each time (if it's even possible in the first place). The idea is so that the user can then copy the url and use it later - kind of like a save state. Any advice on this matter would be much valued, as I am not sure where to look. Thank you for taking the time to read!

ajmajmajma
  • 13,712
  • 24
  • 79
  • 133

1 Answers1

1

You can use $location.search() to read and write attributes to the URL. They would appear as parameters. In your case

http://site/page?mod1=true&mod2=false

To update the values you'd do:

$location.search({mod1: $scope.mod1, mod2: $scope.mod2});

And in your controller when the page is first loaded you read those values back in:

$scope.mod1 = $location.search().mod1;
$scope.mod2 = $location.search().mod2;
Nicholas Hirras
  • 2,592
  • 2
  • 21
  • 28
  • is it possible to run this through ui-routes syntax? – ajmajmajma Feb 27 '15 at 20:14
  • 2
    This is independent of the routing and should work in ui-router or ngRoute. I did see this answer that talks about how to update URL without triggering navigation. might be what you need. https://stackoverflow.com/questions/23585065/angularjs-ui-router-change-url-without-reloading-state – Nicholas Hirras Feb 27 '15 at 20:25
  • AH took me a few minutes of fiddling with it to understand, thank you for your help!! – ajmajmajma Feb 27 '15 at 20:43