我是Angular的新手并尝试制作待办事项列表应用。它是一个简单的应用程序,它使用本地存储在jStorage库的帮助下存储数据。
当我添加任务时,值会更新并显示视图,但它只出现一次。第二次添加数据时,它会添加到localstorage中,但不会显示在视图中。要获取视图,我需要重新加载页面。
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;
});
答案 0 :(得分:1)
在addTask()结束时考虑添加
$scope.tasksObjects = tasksFactory.getData();
我假设视图引用了tasksObjects。