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
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Deepak Mittal
Deepak is a bright web Developer. Deepak has expertise in Java.