$localStroage数组没有't匹配$scope数组
$localStroage array doesn't match $scope array
我正在将对象推送到$localStorage
数组以实现持久性。在添加/删除对象之前,我还检查了这个数组以查看对象是否存在(如果存在,则应为splice
;如果不存在,则为push
)。
当我刷新页面时,$localStorage
返回的数据似乎与预刷新时不同,因为我的检查功能不起作用,尽管在检查时看起来完全相同。
被推送的对象的结构如下:
{
"createdAt": "2015-04-24T10:21:21.649Z",
"difficulty": "Hard",
"exerciseDescription": "Lie on your back on a bench and take hold",
"exerciseID": "3101",
"exerciseName": "Bench Press",
"images": [8679, 8680, 8682],
"tags": ["Barbell", "Horizontal Flexion", "Extension", "Strength", "Chest", "Triceps", "Shoulder", "Elbow, Wrist & Hand"],
"updatedAt": "2015-09-09T20:14:59.681Z",
"words": ["bench", "press", "chest"],
"objectID": "ak6t7ukQdY",
"_highlightResult": {
"exerciseName": {
"value": "Bench Press",
"matchLevel": "none",
"matchedWords": []
}
}
}
检查对象是否存在(切换添加/删除)
$scope.addExerciseToProgramme = function(exercise) {
if (!$localStorage.Programme) {
$localStorage.Programme = [];
}
var index = $localStorage.Programme.indexOf(exercise);
if (index > -1) {
$localStorage.Programme.splice(index, 1);
} else {
$localStorage.Programme.push(exercise);
}
}
监视/加载$localStorage的功能
$scope.$watch(function() {
return $localStorage.Programme
}, function(programme) {
$scope.programme = programme;
});
ng类检查锻炼是否在计划中
<i class="exercise-add-indicator ion-ios-checkmark-outline" ng-class="{'orange': programme.indexOf(exercise) > -1}"></i>
问题
这有两个问题:
刷新后,
ng-class
不会根据我的$scope.programme
的内容有条件地添加类addExerciseToProgramme
函数不尊重indexOf
检查,并将练习对象推送到数组!
Array.prototype.indexOf()
使用严格相等:只有当操作数引用同一Object时,比较Object的表达式才为true
使用localStorage时不应该使用此选项。
将对象保存到localStorage时,它会变成一个字符串。当检索到它时,它又变成了一个对象。
然而,这将是一个新的对象,即使它看起来完全相同。
例如,这将产生false
:
var object1 = { id: 1 };
var object2 = { id: 1 };
console.log(object1 === object2);
为了使它发挥作用,您可以实现一个自定义函数,该函数根据您选择的属性的值检索索引。请注意,它应该是唯一的。
例如:
$scope.getExerciseIndex = function(exercise) {
var index = -1;
if (!$scope.programme) return index;
for (var i = 0; i < $scope.programme.length; i++) {
if ($scope.programme[i].exerciseID !== exercise.exerciseID) continue;
index = i;
break;
}
return index;
};
$scope.exerciseExists = function(exercise) {
var index = $scope.getExerciseIndex(exercise);
return index > -1;
};
$scope.addExerciseToProgramme = function(exercise) {
if (!$localStorage.Programme) {
$localStorage.Programme = [];
}
var index = $scope.getExerciseIndex(exercise);
if (index > -1) $localStorage.Programme.splice(index, 1);
else $localStorage.Programme.push(exercise);
};
HTML:
... ng-class="{'orange': exerciseExists(exercise) }" ...
演示:http://plnkr.co/edit/R6TEisvQ7gkDcwBgw0D1?p=preview
相关文章:
- $scope$观察数组中的一个特定对象——Angular JS控制器
- 为什么AngularJS$scope.watch()在我告诉它监视数组时会停止工作
- 我如何访问在 Angular JS 的 .then() 中定义的$scope数组
- Angular JS(离子)数据数组到$scope元素
- 将数组返回到$scope中;不起作用
- 我正在使用 $scope.$apply 在 http 调用后更新对象数组.谁能告诉我正确的方法
- AngularJS将字符串转换为引用$scope中的对象的对象/数组
- $scope绑定到空值数组 AngularJs
- 尝试将数据数组设置为从视图调用函数后$scope
- AngularJS在$scope数组上使用ng-repeat
- $localStroage数组没有't匹配$scope数组
- 将数组写入$scope(Javascript)
- AngularJS将项目推送到数组的第一个或0索引$scope
- 想要从$scope数组中添加和减去项
- Angular JS:更新$Scope.数组通过用户输入
- $scope数据在angular的数组格式
- 更改重复的$scope数组,但会影响原来的ng-repeat
- 在angular $scope中更新JS数组
- 在AngularJs中不能显示$scope数组对象
- Angular.js视图没有't在更新嵌套的$scope数组时更新