AngularJS 使用 localStorage 存储数据。

时间:2022-01-26 19:43:44
angular.module('starter.mainFactory', [])
.factory('locals',locals) ;locals.$inject = ['$window'];function locals($window){  return{    //存储单个属性    set :function(key,value){      $window.localStorage[key]=value;    },    //读取单个属性    get:function(key,defaultValue){      return  $window.localStorage[key] || defaultValue;    },    //存储对象,以JSON格式存储    setObject:function(key,value){      $window.localStorage[key]=JSON.stringify(value);    },    //读取对象    getObject: function (key) {      return JSON.parse($window.localStorage[key] || '{}');    },    //删除某一键值对    removeItem:function(key){      return $window.localStorage.removeItem(key);    },    //清空整个localStorage    clearLocalStorage:function(){      return $window.localStorage.clear();    }  };}