Maintaining session info in AngularJS

Posted By : Deepak Mittal | 20-Jun-2015

Sessions Info AngularJS

I have started using AngularJS and I'm trying to store user session on my AngularApp.First step to submit username  works. After that, I store the username retrieved from the Factory. On Html page i can display the username  that is stored. You can get the session object on any controller using this
Factory.

 

<html ng-app="angularjs-starter"> <head> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script> </head> <body ng-controller="sessionController">     <h1>Hello {{name}}</h1>     Session object value:      <pre ng-bind='session.data | json'></pre>   </body> 

</html> 

 

var app = angular.module('angularjs-starter', []);

app.controller('sessionController', function($scope, Session) { $scope.name = 'Deepak'; $scope.session = Session; });

app.run(function(Session) {}); 

app.factory('Session', function($http) { var Session = { data: {}, saveSession: function() {  Console.log("save session data in db"); }, updateSession: function() {  console.log( "load data from db");  Session.data = $http.get('sessionData.json').then(function(r) { return r.data;}); } }; Session.updateSession(); return Session; });

Hope this will help you.

THANKS

About Author

Author Image
Deepak Mittal

Deepak is a bright web Developer. Deepak has expertise in Java.

Request for Proposal

Name is required

Comment is required

Sending message..