我有一个存储在本地存储中的数组。一旦用户导航到该数组中的单个项目,他们就可以将项目添加到嵌套数组中:
[{
"id": "1",
"title": "test 1",
"venue": "test 1",
"day": "12",
"month": "January",
"year": "2016",
"date": "2016-02-23T14:53:24.118Z",
"tasks": []
}, {
"id": "2",
"title": "test 2",
"venue": "test 2",
"day": "22",
"month": "April",
"year": "2016",
"date": "2016-02-23T14:53:24.118Z",
"tasks": []
}]
我有2个控制器。一个用ng-repeat列出数组中的所有项目,另一个用于显示单个项目:
.controller('ProfileCtrl', function ($scope, Eventers) {
$scope.eventers = Eventers.all();
})
单项显示:
.controller('ProfileInnerCtrl', function ($scope, $stateParams, $ionicModal, Eventers) {
$scope.eventer = Eventers.get($stateParams.eventerId);
$ionicModal.fromTemplateUrl('new-task.html', function(modal) {
$scope.taskModal = modal;
},
{
focusFirstInput: false,
scope: $scope
});
$scope.createTask = function(task, index) {
$scope.eventer.tasks.push({
title: task.title
});
$scope.taskModal.hide();
Eventers.save($scope.eventer);
$scope.taskModal.hide();
};
})
和我的工厂:
.factory('Eventers', function() {
return {
all: function() {
var eventerString = window.localStorage['eventers'];
if (eventerString) {
return angular.fromJson(eventerString);
}
return [];
},
save: function(eventers) {
window.localStorage['eventers'] = angular.toJson(eventers);
},
newEventer: function(eventerId, eventerTitle,eventerVenue , eventerDay, eventerMonth, eventerYear, eventerDate) {
return {
id: eventerId,
title: eventerTitle,
venue: eventerVenue,
day: eventerDay,
month: eventerMonth,
year: eventerYear,
date: eventerDate,
tasks: []
};
},
get: function(eventerId){
var hell = window.localStorage['eventers'];
var eventers = JSON.parse(hell);
for (var i = 0; i < eventers.length; i++) {
if (parseInt(eventers[i].id) === parseInt(eventerId)){
console.log(eventerId);
return eventers[i];
}
}
return null;
}
}
});
一旦进入单个项目,我希望用户能够打开模态并将项目添加到嵌套任务数组,然后更新本地存储,但我似乎无法绕过它。我现在这样做的方式似乎是添加一个任务,但删除了数组中的所有其他帖子。
答案 0 :(得分:0)
尝试从本地存储中读取所有项目而不仅仅是部分,然后您可以毫无问题地添加,删除或编辑任何内容。之后,您可以继续保存它。
答案 1 :(得分:0)
Eventers.save($scope.eventer);
仅保存当前事件。
虽然实际的实现是试图保存所有
save: function(eventers) {
window.localStorage['eventers'] = angular.toJson(eventers);
},
因此,它会覆盖所有其他事件。