Reputation: 500
Whenever the page is refreshed (Like with F5) the data in my service is cleared. Is there a way to fix this without local storage? It's not a crucial feature but the application breaks if you refresh the page and that's not typically the best for the end user...
First Controller:
$scope.go = function(location){
classService.currentClass = location;
}
Service:
.service('classService', function () {
var classService = this;
//Init
classService.currentClass = null;
});
Second Controller:
.controller('ClassCtrl', function ($scope, classService) {
var currentClass = classService.currentClass;
$scope.className = currentClass.getTeacherClassName();
$scope.classDescription = currentClass.getTeacherClassDescription();
$scope.classCode = currentClass.getTeacherClassCode();
});
It may be blatantly obvious but I'm a bit new to programming and AngularJS in general.
EDIT: I tried removing the null init and it didn't fix it.
Upvotes: 2
Views: 1451
Reputation: 16498
It is very easy to implement $localStorage in angular using ngStorage
please see demo here
http://plnkr.co/edit/F9dEP472n8ETaK1wLqVq?p=preview
Add ngStorage to your home page
<script src="http://rawgit.com/gsklee/ngStorage/0.3.0/ngStorage.min.js"></script>
Add ngStorage to your module
var app = angular.module('app', ['ngStorage']);
Add $localStorage service to your controller and service
angular.module('app').service('classService', function($http,$localStorage)..
the rest is in demo plunkr
Upvotes: 1