1、定义服务
//=========本地存储数据服务============ app.factory('locals', ['$window', function ($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] || '{}');//获取字符串并解析成对象 } } }]);
2、controller中调用:
先注入服务,再set get
app.controller('supplyAddCtrl', function ($scope, $http, $location, locals, $timeout, $compile) { //存 locals.set("firstpos", firstpos);//字符串 locals.setObject("secondpos", secondpos);//对象 //取 locals.get("firstpos"); locals.getObject("secondpos"); }