Dhruv Prakash
Dhruv Prakash

Reputation: 390

Watching local storage using angularjs

I am using a module called ngStorage for handling local Storage operations.(https://github.com/gsklee/ngStorage). Lets say I set an object in local storage $localStorage.something = true; How do I watch this object to find out if it is still available in local storage? I'v tried :

$scope.$watch($localStorage.something,function(newVal,oldVal){
   if(oldVal!==newVal && newVal === undefined){
     console.log('It is undefined'); 
  }
});

Basically I am trying to watch for when a user removes the object from local storage manually through chrome's console.Is this even possible??

Upvotes: 11

Views: 14789

Answers (1)

Marian Ban
Marian Ban

Reputation: 8178

you can try:

$scope.$watch(function () { return $localStorage.something; },function(newVal,oldVal){
   if(oldVal!==newVal && newVal === undefined){
     console.log('It is undefined'); 
  }
})

Upvotes: 22

Related Questions