最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make data in Angular service persist through page refresh - Stack Overflow

programmeradmin5浏览0评论

I have an Angular service that looks like:

var lunchrServices = angular.module('lunchrServices', []);

lunchrServices.service('authService', function () {
    var user = null;

    this.login = function (userEmail) {
        user = userEmail;
    };
    this.logout = function () {
        user = null;
    };
    this.currentUser = function(){
        return user;
    }

});

I use this service on a controller on the main page of my application like so:

var lunchrControllers = angular.module('lunchrControllers', []);

lunchrControllers.controller('MainPageController', ['$scope', '$http', '$state', 'authService',
    function ($scope, $http, $state, authService) {    
        $scope.logIn = function () {    
            $http.post('/api/users/authenticate', {email: $scope.email, password: $scope.password}).
                success(function (data, status, headers, config) {

                    // lines of interest
                    authService.login($scope.email);
                    $state.go('users');
                }).
                error(function (data, status, headers, config) {
                    $scope.errorMessages = data;
                    $scope.password = "";
                })
        }
    }]);

With the users state displaying the following (I'm using ui-router to plug this in a ui-view):

div(class='container')
    h1 Wele {{user}}

    // other stuff

The controller for this page looks like:

lunchrControllers.controller('UserController', ['$scope', '$http', '$state', 'authService',
    function ($scope, $http, $state, authService) {
        $scope.user = authService.currentUser();

        //other stuff
    }]);

When the user taken to this page through the $state.go('users') call, {{user}} is correctly populated.

The problem, however, is that refreshing the page now results in {{user}} being empty. How can I have the data stored in the service persist through page refreshes?

I have an Angular service that looks like:

var lunchrServices = angular.module('lunchrServices', []);

lunchrServices.service('authService', function () {
    var user = null;

    this.login = function (userEmail) {
        user = userEmail;
    };
    this.logout = function () {
        user = null;
    };
    this.currentUser = function(){
        return user;
    }

});

I use this service on a controller on the main page of my application like so:

var lunchrControllers = angular.module('lunchrControllers', []);

lunchrControllers.controller('MainPageController', ['$scope', '$http', '$state', 'authService',
    function ($scope, $http, $state, authService) {    
        $scope.logIn = function () {    
            $http.post('/api/users/authenticate', {email: $scope.email, password: $scope.password}).
                success(function (data, status, headers, config) {

                    // lines of interest
                    authService.login($scope.email);
                    $state.go('users');
                }).
                error(function (data, status, headers, config) {
                    $scope.errorMessages = data;
                    $scope.password = "";
                })
        }
    }]);

With the users state displaying the following (I'm using ui-router to plug this in a ui-view):

div(class='container')
    h1 Wele {{user}}

    // other stuff

The controller for this page looks like:

lunchrControllers.controller('UserController', ['$scope', '$http', '$state', 'authService',
    function ($scope, $http, $state, authService) {
        $scope.user = authService.currentUser();

        //other stuff
    }]);

When the user taken to this page through the $state.go('users') call, {{user}} is correctly populated.

The problem, however, is that refreshing the page now results in {{user}} being empty. How can I have the data stored in the service persist through page refreshes?

Share Improve this question asked Mar 10, 2015 at 3:14 Paymahn MoghadasianPaymahn Moghadasian 10.3k20 gold badges72 silver badges120 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

You can set a cookie or use localStorage. There is a $cookies service in angular.

For a localstorage solution you will have to google around.

shove the user object into a cookie that never expires and then try to read it from there before making your request next time they reload the page.

发布评论

评论列表(0)

  1. 暂无评论