我有一个存储在本地存储中的数组。一旦用户导航到该数组中的单个项目,他们就可以将项目添加到嵌套数组中:
[{
"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;
}
}
});
一旦进入单个项目,我希望用户能够打开一个模式并将项目添加到嵌套任务数组中,然后更新本地存储,但我似乎无法绕开它。我现在这样做的方式似乎添加了一个任务,但删除了数组中的所有其他帖子。