Reputation: 139
I am new to Angular and trying to make a to-do list app. It is a simple app that uses local Storage to store data with the help of jStorage library.
When I add a task, the values get updated and the view appears, but it appears only once. The second time I add the data, it gets added to the localstorage but doesn't appears in the view. To get the view, I need to reload the page.
Controllers.taskController = function($scope, $compile, tasksFactory){
$scope.tasksObjects = tasksFactory.getData();
$scope.priority = 5;
$scope.taskId = 0;
$scope.addTask = function(){
var objectToSend = {};
objectToSend.title = $scope.task.title;
objectToSend.description = $scope.task.description;
objectToSend.priority = $scope.priority;
objectToSend.priorityClass = priorityClassArray[($scope.priority - 1)];
objectToSend.status = $scope.task.status;
objectToSend.taskId = $scope.taskId++;
tasksFactory.postData(objectToSend);
if($scope.tasksObjects === null){
$scope.tasksObjects = [];
}
}
};
angularApp.factory('tasksFactory', function(){
var tasksFactory = {};
tasksFactory.postData = function(ObjectToAdd){
var existingData = $.jStorage.get('tasks');
console.log(existingData);
if(existingData === null){
$.jStorage.set('tasks','[]');
existingData =[];
}
existingData.push(ObjectToAdd);
$.jStorage.set('tasks',existingData);
};
tasksFactory.getData = function(){
return $.jStorage.get('tasks');
}
return tasksFactory;
});
Upvotes: 0
Views: 36
Reputation: 15690
Consider, at the end of addTask(), adding
$scope.tasksObjects = tasksFactory.getData();
I'm assuming that the view is referencing tasksObjects.
Upvotes: 1