L
L
leo_fr2016-01-21 17:15:52
Angular
leo_fr, 2016-01-21 17:15:52

How to properly use localStorage in Angularjs?

Good day, I can't figure out how to use localStorage in angularjs correctly. The crux of the problem lies in how to use it correctly. Below is the source of the request.
loginService

'use strict';

angular.module('prApp')
    .factory('loginService',
    ['Base64','$http', '$cookieStore','$rootScope',
    function (Base64,$http, $cookieStore,$rootScope) {
        var service={};

        service.Login=function(username, password, callback){
            $http.post('/auth/log', { username: username, password: password })
                .success(function (response ) {
                    callback(response);
                })
                .error(function (response) {
                    callback(response);

                });
        };

266430d0ca914e11958d10c433a9ecb9.png
1.2 request, response
loginCtrl
'use strict';

angular.module('prApp')
    .controller('loginCtrl',
    ['$scope','$rootScope','$location', 'loginService',
    function ($scope,$rootScope, $location,loginService) {

        loginService.ClearCredentials();

        $scope.login = function(){
            $scope.dataLoading = true;
            loginService.Login($scope.username, $scope.password, function(response){
                if(response.success){
                    loginService.SetCredentials($scope.username, $scope.password);




                    $location.path('/user');
                } else {
                    $scope.error = response.message;
                    $scope.dataLoading = false;
                }
            });
        };
    }]);

dd37ac202c774fa5bbeb29afc339d678.png
The idea is that when we log in before going $location.path('/user'); should happen, for example, saving id user-a to the session and then on the opened user page, I can use the id for a new request, for example. I will be glad for any help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anveed, 2016-01-21
@Anveed

Use the module for angular:
https://github.com/grevory/angular-local-storage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question