[英]In angularjs how do you catch an error for ngStorage when localStorage is full?
I am saving several items to localStorage
using ngStorage
in AngularJS, but if the user's localStorage
is full how can I catch that error and allow the JavaScript to keep running? 我正在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');
}
Perhaps not specific to AngularJS, you should be able to get some detailed information for your localStorage
with the following. 也许不是特定于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');
JSFiddle Link - demo JSFiddle Link-演示
See this answer as well for an exception driven approach. 对于异常驱动方法,也请参见此答案 。 The referenced answer will share how you can force an exception you can likely catch for.
引用的答案将分享您如何强制可能会捕获的异常。 The code above may allow you to perform/bypass certain logic if you are running low.
上面的代码可以让您在电量不足时执行/跳过某些逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.