BlondePainter BlondePainter - 19 days ago 5
AngularJS Question

store json value as session in angular

I have a php file which returns json result based from a $http post as seen here:

json result:

now, in my controller below, i want to store

03-1314-00916
or
data[0]['student_number']
as a session in angular so i can use it anywhere in any of my controllers. How will i make it.



app.controller('signInCtrl', function($scope, $stateParams, $http) {
$scope.txtUsername = '';
$scope.txtPassword = '';

$scope.signIn = function(){
$http.post('http://infosys.esy.es/ion/json_login.php',
{'username' : this.txtUsername, 'password': this.txtPassword})
.success(function(data){
if(data[0]['logged'] === true)
{
$scope.unable = false;
console.log(data[0]);
console.log(data[0]['student_number']);
here --> someSessionVar = data[0]['student_number'];
//window.location = '#/app/dashboard';
}else{
//console.log('unable to login');
$scope.unable = true;

}
//console.log(data[0]['logged']);
})
}
}) //end controller




Answer

Use HTML5 sessionStorage

if(data[0]['logged'] === true)
  {
    $scope.unable = false;
    console.log(data[0]);
    console.log(data[0]['student_number']);
    sessionStorage.setItem('student_number', data[0]['student_number']);      
    //window.location = '#/app/dashboard';
  }

To retrieve

   $scope.studentNumber = sessionStorage.getItem('student_number');
Comments