我正在尝试控制 $scope.$watch 功能。目前,我的迭代中有一个无限循环。
$scope.$watch('dashboards', function(newVal, oldVal) {
if (newVal !== oldVal) {
$scope.dashboard = $localStorage.sample;
$scope.dashboards['1'].id = $scope.dashboards[1].id + 1;
$localStorage.sample = $scope.dashboards[1];
console.log('newval: '+$scope.dashboards['1'].id);
} else {
$scope.$storage = $localStorage;
$localStorage.sample = $scope.dashboards[1];
$scope.dashboard = $localStorage.sample;
$scope.dashboards['1'].id = $scope.dashboards[1].id.length + 1;
$localStorage.sample = $scope.dashboards[1];
console.log('oldval: '+$scope.dashboards['1'].id);
}
}, true);
这是我的 scope.dashboards:
$scope.dashboards = {
'1' : {
id : '1',
widgets : [{
code : "bla1.html",
col : 0,
row : 0,
sizeY : 1,
sizeX : 2,
title : "Bla1"
}, {
code : "bla2.html",
col : 2,
row : 0,
sizeY : 2,
sizeX : 2,
title : "Bla2"
}, {
code : "bla3.html",
col : 0,
row : 4,
sizeY : 1.5,
sizeX : 2,
title : "Bla3"
}
}
我想要完成的是,每当我的 $scope.dashboards 中的小部件位置发生变化时,我都会在 localStorage 中存储一个新 ID 以保存其位置状态,以便在浏览器关闭/刷新时它仍然会保留其会议/职位。也许您对我将如何解决这个问题有所了解?谢谢!