[英]How to access localstorage by JavaScript which is created by ngStorage in AngularJS
[英]In angularjs how do you catch an error for ngStorage when localStorage is full?
我正在AngularJS中使用ngStorage
將多個項目保存到localStorage
,但是如果用戶的localStorage
已滿,我如何捕獲該錯誤並允許JavaScript繼續運行?
try {
$scope.$storage.gridObject = $scope.gridConfig.data;
$scope.$storage.dataStored=true;
}
catch(err) {
//failed to save to local storage, try clearing localStorage for IAP and throw error
$scope.$storage.dataStored=false;
$scope.$storage.$reset({
dataStored: false,
gridObject: {}
})
console.log('error occured message');
}
也許不是特定於AngularJS的,您應該能夠通過以下方式獲取有關localStorage
一些詳細信息。
var allocated = 5; // default 5MB allocation
var total = 0;
for(var x in localStorage) {
var amount = (localStorage[x].length * 2) / 1024 / 1024;
total += amount;
}
var remaining = allocated - total;
console.log( 'Used: ' + total + ' MB');
console.log( 'Remaining: ' + remaining + ' MB');
對於異常驅動方法,也請參見此答案 。 引用的答案將分享您如何強制可能會捕獲的異常。 上面的代碼可以讓您在電量不足時執行/跳過某些邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.