How to show data from a Session in Angularjs?

Asked

Viewed 954 times

2

I’m making a mobile app I already have login working and saving the values I want in Sesssion now I don’t know and how to show these values in 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>

1 answer

0


within your controller, after its 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' property, which you created to perform the bind in html.

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

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


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

}
  • It worked well was really a simple thing that I was complicating. Thank you

  • needing am at my disposal @Césarsousa

Browser other questions tagged

You are not signed in. Login or sign up in order to post.