为什么数组在$localStorage工作
Why is an array in $localStorage working
index.html
<div class="modal-body">
<form >
<tags options="{addable: true}" typeahead-options="typeaheadOpts" data-model="information.address" data-src="toPerson as toPerson for toPerson in to"></tags>
<input type="text" placeholder="Subject" style="width:95%;" data-ng-model = "information.subject"><br />
<textarea style="width:95%;" rows="10" data-ng-model = "information.emailContent"></textarea>
</form>
</div>
<div class="modal-footer">
<a href="#" class="btn" ng-click = "submit(information.address, information.subject, information.emailContent); close()">Close</a>
<a href="#" class="btn btn-primary" ng-click = "close()">Send</a>
</div>
emailViewController.js
$scope.information = {
address: [],
subject: []
};
$scope.submit = function (address, subject, emailContent) {
$localStorage.address.push(address);
if (! ($localStorage.subject instanceof Array) ) {
$localStorage.subject = [];
}
$localStorage.subject.push(subject);
如果我不将$localStorage.subject
初始化为数组,它会给我一个错误,说.push is not a function
。
当我将数据推送到$localStorage.address
时,为什么不给出此错误?
您使用:
var Helper = {
pushArray: function (arr1, arr2) {
arr1 = this.toArray(arr1);
if (arr1 && arr2 && Array.isArray(arr1)) {
arr1.push.apply(arr1, Array.isArray(arr2) ? arr2 : [arr2]);
}
},
toArray: function (arr) {
return arr ? (Array.isArray(arr) ? arr : [arr]) : [];
}
};
Helper.pushArray(arr1, second);
或者直接声明:
$localStorage.$default({
subject: []
});
相关文章:
- $localStorage array select with ng选项工作不正常
- 使ng-show/ng-hide与由Angular之外的外部源修改的localStorage一起工作
- localstorage.getData 在 Phone gap 中无法与 JSON 正常工作
- window.localStorage 未按预期工作
- localStorage和jQuery(无工作css)
- 如何让localStorage在todolist应用程序上工作
- 可以'不能让js模型与localStorage一起工作,或者根本不能
- localStorage getItem不能很好地工作
- 无法使localStorage工作
- localstorage事件监听器不能在Microsoft Edge上工作
- 为什么$cookies和$localStorage不能一起工作?
- LocalStorage在IE 11 win8.1下不工作
- localStorage不能在Edge中工作
- 检查非空不与localStorage工作
- localStorage在Javascript中是如何工作的
- 为什么数组在$localStorage工作
- HTML5 localStorage不能在Android WebView上工作
- Angularjs的localstorage不能正常工作
- Javascript / jQuery与localStorage API不工作
- LocalStorage不能在iPhone上工作-不能在私人浏览