检查本地存储是否已成功更改

时间:2014-06-04 12:22:08

标签: angularjs local-storage

我正在使用LocalStorageModule

myApp.factory('myService', function($rootScope, localStorageService) {
    return {
      addSomething : function(name) {
        ...
        var ret = localStorageService.set('something', lotsOfData);
        alert(ret);
        return ret;
      }
    }
 }

在这种情况下,rettrue,即使数据尚未成功添加或未更改。有没有办法检查添加是否有效?

1 个答案:

答案 0 :(得分:0)

您只需读取存储在本地存储中的数据,并将其与预期值进行比较:

localStorageService.set('foo', foo);
if (!angular.equals(localStorageService.get('foo'), foo)) {
    throw "The data hasn't been successfully added to the local storage.";
}