ajithes1
ajithes1

Reputation: 427

How can i do session handling in angularjs with javascript?

There are only two points in the application where the login should appear:

  1. When I am not logged in and I attempt to visit a page that requires login, e.g. my profile page.
  2. When I attempt to make a request that requires a login, e.g. my session has expired whilst I’m attempting to post something.

Upvotes: 0

Views: 75

Answers (2)

Leasye
Leasye

Reputation: 279

With angularJS :

  • To save value : $window.sessionStorage.setItem('key', 'value')
  • To retreive value : $scope.name = $window.sessionStorage.getItem('key')

Here is a small example : http://jsfiddle.net/gioeleslfierro/s0pm6gcq/

I suggest you to use the angular way with $window. But if you want to do it by yourself its pretty similar (source) :

// Save data to sessionStorage
sessionStorage.setItem('key', 'value');

// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');

// Remove saved data from sessionStorage
sessionStorage.removeItem('key');

// Remove all saved data from sessionStorage
sessionStorage.clear();

Upvotes: 1

Raja Ramachandran
Raja Ramachandran

Reputation: 456

use HtML5 $sessonStorage or $localStorage.

Example
--------
sessionStorage.setItem('id', userId);
sessionStorage.getItem('id');

Upvotes: 1

Related Questions