How to show session data in angularjs?

2

I'm making a mobile app I already have the login working and saving the values I want in session now I do not know and how to show these values on the page after login.

Controller

.controller('LoginInterno', function($scope, $http, sessionService) {
    $scope.Btnlogin= function (input){
        $http.post("https://www.sabeonde.pt/api/api_login.php?email=" + input.email + "&password=" + input.password).success(function (data) {
            $scope.login = data;
            userInfo = {
                user_id: data.user_id,
                nome: data.nome,
                user_foto: data.user_foto,
                user_slug: data.user_slug
            };
            sessionService.set('user_id',userInfo.user_id);
            sessionService.set('nome',userInfo.nome);
            sessionService.set('user_foto',userInfo.user_foto);
            sessionService.set('user_slug',userInfo.user_slug);
            window.location = "#/app/home"
        }).
        error(function (data) {
            alert("Dados Incorrectos");
        });
    };
})

sessionService.js

app.factory('sessionService', ['$http', function($http){
    return{
        set:function(key,value){
            return sessionStorage.setItem(key,value);
        },
        get:function(key){
            return sessionStorage.getItem(key);
        },
        destroy:function(key){
            $http.post('data/destroy_session.php');
            return sessionStorage.removeItem(key);
        }
    };
}])

User details

<div class="item item-avatar" ng-controller="LoginInterno">
    <img src="mcfly.jpg">
    <h2>{{nome}}</h2>
    <p>0 Opiniões, 0 Seguidores</p>
</div>
    
asked by anonymous 16.09.2015 / 22:26

1 answer

0

Within your controller, after your function, BtnLogin, add a call to your systemService service by calling the 'get' function you created by passing the key (name) you created in sessionStorage, and assigning in the 'name' , which you created to bind in the html.

.controller('LoginInterno', function($scope, $http, sessionService) { 

   $scope.Btnlogin = function () {
           //Seu codigo.
   }


   $scope.nome = sessionService.get('nome');

}
    
17.09.2015 / 03:19