【问题标题】:AngularJS - Retrieve localStorage in moduleAngularJS - 在模块中检索 localStorage
【发布时间】:2019-05-01 10:11:08
【问题描述】:

登录后,我将用户数据存储在 localStorage 中,并将其重定向到 Dashboard。 登录控制:

    (function() {
    'use strict';

    angular.module('BlurAdmin.pages.login')
        .controller('LoginCtrl', LoginCtrl);

    /** @ngInject */
    function LoginCtrl($scope, $timeout, $http, $location, toastr) {

        $scope.login = function() {

            var data = { email: $scope.email, senha: $scope.password }

            $http.post('http://xxxxxxx/snaapp/admin/login', data).
            then(function(response) {
                localStorage.token = response.data.token;
                $http.get('http://xxxxxxx/snaapp/auth/user/info', { headers: { 'Authorization': response.data.token } }).
                then(function(response) {

                    //Set values in localStorage

                    localStorage.user = JSON.stringify(response.data);
                    $location.path("/dashboard");
                }).catch(function(fallback) {
                    toastr.error('Erro ao fazer login');
                });

            }).catch(function(fallback) {
                toastr.error('Erro ao fazer login');
            });
        };

    }
})(); 

如何从特定模块的 localStorage 中检索数据?

    (function() {
    'use strict';

    angular.module('BlurAdmin.pages.juridico', [
            'BlurAdmin.pages.juridico.acoesColetivas'
        ])
        .config(routeConfig);

    /** @ngInject */
    function routeConfig($stateProvider) {

        //I need to do something like this:
        console.log(localStorage.user)

        $stateProvider
            .state('juridico', {
                url: '/juridico',
                template: '<ui-view  autoscroll="true" autoscroll-body-top></ui-view>',
                abstract: true,
                title: 'Jurídico',
                sidebarMeta: {
                    icon: 'ion-gear-a',
                    order: 100,
                },
            });
    }

})();

上面的代码只有在我重新加载页面时才有效,但这不会发生,一旦它被重定向,我需要在模块中检索这些数据

【问题讨论】:

  • 代码在哪里存储和检索?
  • 我编辑了我的问题

标签: javascript angularjs


【解决方案1】:

存储在本地存储中

let userData = JSON.stringify(response.data);
localStorage.setItem("user", userData);

从本地存储中检索

let savedUser = localStorage.getItem("user");

参考:link

【讨论】:

  • 我试过了,但还是不行。如果我重新加载页面,只需加载 localStorage
【解决方案2】:

您需要将$window 注入您的控制器,您可以使用$window.localStorage

要检查它,您可以按照 Chrome -> F12 -> 应用程序 -> 存储 -> 本地存储

(function() {
  'use strict';

  angular.module('BlurAdmin.pages.login')
    .controller('LoginCtrl', LoginCtrl);

  /** @ngInject */
  function LoginCtrl($scope, $timeout, $http, $location, toastr, $window) {

    $scope.login = function() {

      var data = {
        email: $scope.email,
        senha: $scope.password
      }

      $http.post('http://xxxxxxx/snaapp/admin/login', data).
      then(function(response) {
        $window.localStorage.token = response.data.token;
        $http.get('http://xxxxxxx/snaapp/auth/user/info', {
          headers: {
            'Authorization': response.data.token
          }
        }).
        then(function(response) {

          //Set values in localStorage

          $window.localStorage.user = JSON.stringify(response.data);
          $location.path("/dashboard");
        }).catch(function(fallback) {
          toastr.error('Erro ao fazer login');
        });

      }).catch(function(fallback) {
        toastr.error('Erro ao fazer login');
      });
    };

  }
})();

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多