Access localStorage variable data in other controllers

2.4k views Asked by At

Under the controller named usersConrtoller.js I'm storing some object data in the localStorage using ngStorage.

$scope.users = [{
        "entity$": "-/sys/daily_journeys",
        "name": "Tom",
        "walked": 90,
        "traveled_distance": "773",
        "total_cout": 0.367,
        "days": 22,
        "avg_risk": "0.0",
        "risk_category": "GREEN"
}]

saved to local storage:

$localStorage.users = $scope.users;

How can I access $localStorage.users object's data in other controllers?

1

There are 1 answers

0
maurycy On

As far as I know the ngStorage is an injectable service so in other controller you just need to inject the service and read it

$scope.users = $localStorage.users

or you might need to watch over it to see if it's updated

$scope.$watch(function(){
  return $localStorage.users
}, function (users) {
  $scope.users = users;
})