我正在构建一个chrome扩展,并遇到了一个我无法绕过头的bug.问题是单个对象属性在chromes的存储中变为null.
我正在测试这个:
console.log("pre-storage",settings); var obj = {}; obj[storage_key] = settings; chrome.storage.sync.set(obj,function() { chrome.storage.sync.get(storage_key,function(data) { console.log("post-storage",data[storage_key]); }); });
这是输出:
pre-storage,Object { ... soundClip: Object { options: Array[5],selected: Object { label: "Soft2",value: "snd/soft2.wav" } } } post-storage,selected: null } }
直接存储JSON.parse(JSON.stringify(obj))代替obj似乎解决了这个问题.任何人有什么想法可能导致这个?任何帮助表示赞赏!
编辑:制作obj的深层副本并不能解决问题.
Edit2:我应该扩展settings.soundClip的设置方式.我正在使用Angular(1.x),我正在使用自定义选择指令.剥离指令看起来像这样:
function mySelect() { return { restrict: "E",templateUrl: "mySelect.html",scope: { options: "=",selected: "=" },link: function (scope) { scope.select = function (item) { scope.selected = item; }; } } }
指令模板视图(mySelect.html):
<div> <div ng-repeat="item in options track by $index" ng-click="select(item)"> </div> </div>
然后属性是双向绑定的,如下所示:
<my-select selected="settings.soundClip.selected" options="settings.soundClip.options"> </my-select >