使用angularjs观看本地存储

Dhr*_*ash 11 local-storage angularjs

我正在使用一个名为ngStorage的模块来处理本地存储操作.(https://github.com/gsklee/ngStorage).假设我在本地存储中设置了一个对象$ localStorage.something = true; 如何查看此对象以确定它是否仍可在本地存储中使用?我试过了:

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

基本上我正在努力观察当用户通过chrome的控制台手动从本地存储中删除对象时.这甚至可能吗?

Mar*_*Ban 22

你可以试试:

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

  • 这有效,但 `newVal !== undefined` 不是 `newVal === undefined` (2认同)