0

I want to select a person from the list and update the URL to get a shareable URL.

<ul>
    <li ng-repeat="ctrl.persons as person" ng-click="ctrl.details(person)>{{person.name}}</li>
</ul>

<div class="details">{{ctrl.active_person.name}}</div>

in the controller

vm.persons = [{id: 1, name: "john"}, {id: 2, name: "george"}, {id: 3, name: "michael"}];
vm.details = function(person) {
    $location.path('/persons/' + person.id);
    vm.active_person = person;
}

I´m successfully updating the URL when I select a person from the list. te problem is that If I reload the page the selection is lost. how can I make vm.active_person = the person with the id in the url ?

notes: In this example I added an array with 3 persons but in the real code that array is coming from a service (with many many many persons)

thanks so much!

handsome
  • 2,335
  • 7
  • 45
  • 73
  • 1
    Which router are you using? – charlietfl May 29 '17 at 00:19
  • I´m using https://docs.angularjs.org/api/ngRoute – handsome May 29 '17 at 00:21
  • 2
    Go through tutorial...shows how to use `$routeParams`. Use that to filter data for person needed – charlietfl May 29 '17 at 00:22
  • If you have the person Id that mean you can get user information in next page from your database, it's normal if after changing the url you lose the selected person – Maher May 29 '17 at 01:42
  • you can use this way [linking-one-controller-to-another-to-call-service-on-ng-click](https://stackoverflow.com/questions/34668416/linking-one-controller-to-another-to-call-service-on-ng-click/34702780#34702780) – Maher May 29 '17 at 01:56
  • Are you using HTML5 mode? – Phil May 29 '17 at 05:35
  • as javascript is stateless and angular too is stateless. you can write a service that will use sessionStorage to store and retrieve your data. so When you are selecting person you can store that into sessionStorage and when you are refreshing you can get the stored person data and update the url as well – Kanagu May 29 '17 at 06:30

0 Answers0