当浏览器刷新本地存储刷新时
When browser refresh localStorage flushes
我现在正在修补这段代码,我想利用 localStorage 将我的小部件的状态保存在我的仪表板上,以便当用户返回我的应用程序时,小部件的位置完好无损并保存,但每次我刷新浏览器时,它都会回到其当前的 scope.dashboards 状态,我不知道我将如何修复。 我使用ngStorage模块进行本地存储
var modInProgr = false;
$scope.$watch("dashboards['1'].widgets", function(newVal, oldVal) {
if (!modInProgr) {
modInProgr = true;
// modify dashboards
$scope.$storage = $localStorage;
$localStorage.sample = $scope.dashboards[1];
console.log($localStorage.sample);
}
$timeout(function() {
modInProgr = false;
}, 0);
$scope.dashboard = $localStorage.sample;
}, true);
// init dashboard
$scope.dashboard = $localStorage.sample;
我有一个按钮,用户单击该按钮来调用此函数:
//Used to save the dashboard to BOTH local storage and PENN database
//Local storage will attempt to be loaded first. However, if local storage is not there
//then we will load the dashboard from the database
$scope.serialize = function() {
$scope.dashboardJSON = angular.toJson($scope.standardItems);
console.log($scope.dashboardJSON);
localStorageService.set("DashboardInfo", $scope.dashboardJSON);
//Send HTTP request 'POST' to saveDashboard function in Rails controller
$http({
method: 'POST',
url: 'saveDashboard',
data: {'dashboardInfo': $scope.dashboardJSON },
headers: {'Content-Type': 'application/json' }
}).success(function(data, status, headers, config)
{
//Let user know that their dashboard was saved with this success flash
$scope.successSavingDashboardToDBAlert();
}).error(function(data, status, headers, config)
{
//Let the user know the dashboard could not be saved with this error flash
$scope.errorSavingDashboardToDBAlert();
});
};
这会将其保存到本地存储中。我还将其保存到数据库中,以防本地存储过期或删除。
然后当网格加载时,我这样做:
var dashboardInfo = localStorageService.get("DashboardInfo");
if (dashboardInfo == null)
{
//Load from the database
}
else
{
//console.log("Loading from Local Storage");
//Parse the local storage JSON data with angular
var parsedDashboard = angular.fromJson(dashboardInfo);
//Loop through the parsed data to push it to the array that is used for gridster.
//Usually this is called items but in my case I called it standardItems
for(var i = 0; i < parsedDashboard.length; i++)
{
//console.log(parsedDashboard[i]);
$scope.standardItems.push(parsedDashboard[i]);
}
//$scope.successAlert();
}
相关文章:
- 在刷新之前存储元素中的数据
- ExtJS网格在存储更新后未刷新
- 页面刷新时从本地存储恢复表单
- 将图像从本地存储加载到画布仅每秒刷新一次
- 如何刷新存储和快照的 jquery 选择器变量
- 网格存储不会在 ExtJS 5 中刷新
- 页面刷新后的 Javascript 存储变量
- 如何存储元素是否被选中,以便页面在刷新时记住它
- 刷新后重置本地存储值
- OAuth2 刷新令牌.如何在客户端存储它
- 当浏览器刷新本地存储刷新时
- 如何在刷新后将本地存储值保留在 html 中
- React Flux 存储和硬刷新
- angularjs本地存储防止页面刷新
- 如何在内存存储更改后刷新dojo-gridx
- 如何在浏览器关闭但不刷新时清除本地存储
- 存储和增加数字,即使在刷新页面后
- 刷新后如何在本地存储中呈现所有密钥
- 当“从属下拉列表”刷新页面时,将输入框值存储为变量
- firebase存储中的更新记录只有在刷新页面后才可见