1

我正在保存几个项目以在 AngularJS 中使用,但如果用户localStorage已满,我如何才能捕捉到该错误并允许 JavaScript 继续运行?ngStoragelocalStorage

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');  
}
4

1 回答 1

0

也许不是特定于 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 链接- 演示

有关异常驱动的方法,请参阅此答案。引用的答案将分享您如何强制可能捕获的异常。如果您运行不足,上面的代码可能允许您执行/绕过某些逻辑。

于 2015-06-11T22:38:37.413 回答