访问其他控制器中的localStorage变量数据

时间:2015-06-21 19:35:12

标签: javascript angularjs session local-storage ng-storage

在名为usersConrtoller.js的控制器下,我使用localStoragengStorage中存储了一些对象数据。

$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"
}]

保存到本地存储:

$localStorage.users = $scope.users;

如何在其他控制器中访问$localStorage.users对象的数据?

1 个答案:

答案 0 :(得分:1)

据我所知,ngStorage是一种可注射服务,所以在其他控制器中你只需要注入服务并阅读它

$scope.users = $localStorage.users

或者您可能需要查看它是否已更新

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