i've got problem need in angular 1.4.0. have factory in angular has complex structure , method within able restore factory original state calling method. factory looks this.
angular.module('myapp').factory('itemfcty', function(){ var currentvalues = {}; var default values { = 1, b = 2, resetdata : function(){ currentvalues = angular.extend(currentvalues, defaultvalues); return currentvalues }; }; defaultvalues.resetdata(); return currentvalues;
}); in order add values 'a' call itemfcty.a = 2; method works when want overwrite values , when required. have been asked persist data on refresh. stringify object json. this:
localstorage.setitem('itemfcty', json.parse(itemfcty);
however have hit snag. data stored in local storage
{a = 1,b = 2,}
i can add method in doing this:-
itemfcty.resetdata = function(){return currentvalues = angular.extend(currentvalues, defaultvalues);}
this issue factory function same way before not able call function call , return outside default values object not there more can cannot life of me work out how add goes directly object whole.
your appreciated!
/*************************edit *****************************/ ok, think havent explained point well. factory looks above. user hits refresh. factory stored in local storage. local storage. heres issue. looks before local storage
angular.module('myapp').factory('itemfcty', function(){ var currentvalues = {}; var defaultvalues = { : 1, b : 2, resetdata : function(){ angular.extend(currentvalues, defaultvalues); // don't have return values } // <------you can't use ; in object properties }; defaultvalues.resetdata(); return currentvalues; });
now when data out f local storage , factory factory looks this.
angular.module('myapp').factory('itemfcty', function(){ : 1, b : 2, });
i can add reset data function in, factory not contain current or default values, reset data function not work. asking how make factory, same after have reloaded data local storage.
did load variable localstorage? also, there typo, i'd var default values
. in general stringifying not account methods, because wouldn't make sense other languages importing them.
Comments
Post a Comment