首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AngularJS -在模块中检索localStorage

AngularJS -在模块中检索localStorage
EN

Stack Overflow用户
提问于 2019-05-01 18:11:08
回答 2查看 42关注 0票数 0

登录后,我将用户数据存储在localStorage中,并将其重定向到仪表板。LoginCtrl:

代码语言:javascript
运行
复制
    (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检索数据?

代码语言:javascript
运行
复制
    (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,
                },
            });
    }

})();

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

EN

回答 2

Stack Overflow用户

发布于 2019-05-01 19:03:50

存储在本地存储中

代码语言:javascript
运行
复制
let userData = JSON.stringify(response.data);
localStorage.setItem("user", userData);

从本地存储中检索

代码语言:javascript
运行
复制
let savedUser = localStorage.getItem("user");

参考:link

票数 0
EN

Stack Overflow用户

发布于 2019-05-01 19:16:52

你需要在你的控制器中注入$window,你可以使用$window.localStorage

要查看它,您可以使用Chrome -> F12 ->应用程序->存储->本地存储

代码语言:javascript
运行
复制
(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');
      });
    };

  }
})();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55934644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档