[英]Angular Factory Object Persisting Over Storing in Local Storage
I've got a problem that i need help with in Angular 1.4.0. 我有一个需要在Angular 1.4.0中获得帮助的问题。 I have a factory in angular which has a complex structure and method within so i am able to restore the factory back to its original state by calling the method. 我有一个工厂,工厂内有一个复杂的结构和方法,因此我可以通过调用该方法将工厂恢复到原始状态。 The factory looks like this. 工厂看起来像这样。
angular.module('myApp').factory('itemFcty', function(){
var currentValues = {};
var default values {
a = 1,
b = 2,
resetData : function(){
currentValues = angular.extend(currentValues, defaultValues);
return currentValues
};
};
defaultValues.resetData();
return currentValues;
}); }); In order to add values to 'a' i call itemFcty.a = 2; 为了将值添加到“ a”,我调用itemFcty.a = 2; So this method works well when i want to overwrite all the values as and when required. 因此,当我想按需覆盖所有值时,此方法效果很好。 However i have been asked could i persist the data over a refresh. 但是,有人问我是否可以刷新保存数据。 So i stringify the object into JSON. 所以我将对象字符串化为JSON。 Like this: 像这样:
localStorage.setItem('itemFcty', JSON.parse(itemFcty); localStorage.setItem('itemFcty',JSON.parse(itemFcty);
However i have hit a snag. 但是我遇到了障碍。 The only data to be stored in the local storage is the 唯一要存储在本地存储中的数据是
{a = 1,b = 2,}
I can add the method back in by doing this:- 我可以通过以下方法重新添加该方法:
itemFcty.resetData = function(){return currentValues = angular.extend(currentValues, defaultValues);}
This is the issue that now the factory does function the same way as before as i am not able to call the function as the call and return outside the default values object is not there any more i can cannot for the life of me work out how to add it back into as everything goes directly into the object as a whole. 这是一个问题,现在工厂的功能与以前相同,因为我无法像以前那样调用该函数并在默认值对象之外返回,因此我无法再为我的生活工作了将其添加回去,因为一切都直接进入整个对象。
Your help would be greatly appreciated! 您的帮助将不胜感激!
/*************************EDIT *****************************/ Ok, so i think that i havent explained the point very well. / *************************编辑*********************** ****** /好的,所以我认为我没有很好地解释这一点。 My factory looks exactly like the above. 我的工厂看起来和上面一样。 The user hits refresh. 用户点击刷新。 The factory is stored in local storage. 工厂存储在本地存储中。 I get it back from local storage. 我从本地存储取回它。 But heres the issue. 但是这里的问题。 It looks like this before local storage 本地存储之前看起来像这样
angular.module('myApp').factory('itemFcty', function(){
var currentValues = {};
var defaultValues = {
a : 1,
b : 2,
resetData : function(){
angular.extend(currentValues, defaultValues);
// you don't have to return the values
} // <------you can't use ; in the object properties
};
defaultValues.resetData();
return currentValues;
});
Now when i get the data out f local storage and into the factory the factory then looks like this. 现在,当我将数据从本地存储中取出并送入工厂时,工厂看起来像这样。
angular.module('myApp').factory('itemFcty', function(){
a : 1,
b : 2,
});
I can add the reset data function back in, however as the factory does not contain current or default values, the reset data function will not work. 我可以重新添加重置数据功能,但是由于工厂不包含当前值或默认值,因此重置数据功能将无法使用。 So basically i am asking how to make my factory, look the same as it does originally after i have reloaded data from the local storage. 因此,基本上,我在问如何使我的工厂与从本地存储中重新加载数据后的外观相同。
Did you load the variable back from localStorage? 您是否从localStorage加载了变量? Also, there is a typo, I'd say var default values
. 另外,还有一个错字,我会说var default values
。 In general stringifying does not account for methods, because that wouldn't make sense for other languages importing them. 通常,字符串化不会考虑方法,因为其他语言导入方法没有意义。
Problems: 问题:
var default values {
, space separated variable and missing assignment operator =
. var default values {
,空格分隔的变量,缺少赋值运算符=
。 =
but :
, so it will produce error { a = 1, b = 2 }
. 在对象中,您可以使用=
but :
分配值,因此将产生错误{ a = 1, b = 2 }
。 what you can do is: 您可以做的是:
angular.module('myApp').factory('itemFcty', function(){
var currentValues = {};
var defaultValues = { // <-------should have to be declared like this
a : 1, // = should be replaced with :
b : 2, // and here too.
resetData : function(){
angular.extend(currentValues, defaultValues);
// you don't have to return the values
} // <------you can't use ; in the object properties
};
defaultValues.resetData();
return currentValues;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.