AngularJS-我不明白“工厂”是如何运作的
我得到了这个服务,我把所有关于检索、删除和添加任务的http请求都放在了这个服务中。任务的删除和检索工作正常,但当我添加任务或更新现有任务时,任务更改不会显示出来。我必须刷新浏览器 我只是不明白为什么我似乎不能让我的任务添加工作。如果有人能帮我写代码,我将非常感激 我制作了一个GIF,在这个例子中,我在更新任务时演示了我的问题 () my app.js代码的一部分:AngularJS-我不明白“工厂”是如何运作的,angularjs,Angularjs,我得到了这个服务,我把所有关于检索、删除和添加任务的http请求都放在了这个服务中。任务的删除和检索工作正常,但当我添加任务或更新现有任务时,任务更改不会显示出来。我必须刷新浏览器 我只是不明白为什么我似乎不能让我的任务添加工作。如果有人能帮我写代码,我将非常感激 我制作了一个GIF,在这个例子中,我在更新任务时演示了我的问题 () my app.js代码的一部分: zazzleApp.factory('TaskService', function ($http) { var T
zazzleApp.factory('TaskService', function ($http) {
var TaskService = {};
TaskService.taskList = [];
TaskService.addTask = function(pTask){
var newClickDate = clickDate;
console.log('LOGGGING NEW CLICK DATE = ', clickDate);
var newEditId = editId;
console.log('LOGGGING NEW edit id = ', newEditId);
var url;
console.log('edit id = ' , newEditId);
if (newEditId) {
url = 'api/task/update/' + newEditId;
} else {
url = 'api/task/create';
}
console.log("URL URL USA", url, newEditId);
defaultStart = new Date(newClickDate);
defaultStart = defaultStart.getFullYear() + "-" + (defaultStart.getMonth() + 1) + "-" + defaultStart.getDate();
defaultStart += " 00:00:00";
defaultEnd = new Date(newClickDate).addDays(1);
defaultEnd = defaultEnd.getFullYear() + "-" + (defaultEnd.getMonth() + 1) + "-" + defaultEnd.getDate();
defaultEnd += " 00:00:00";
console.log('LOGGING DEFAULT START AND DEFAULT END ' , defaultStart, defaultEnd);
pTask.color = $('#containerColorPicker').attr('ng-data-id');
return $http.post(url, {
'name': pTask.project_name,
'project_id': pTask.project_type,
'location_id': pTask.location,
'estimate_time': pTask.estimate_time || 2,
'project_client_name': pTask.project_client_name,
'url': pTask.url,
'resource_link': pTask.resource_link,
'notes': pTask.notes,
'start_time': pTask.start_time || defaultStart,
'end_time': pTask.end_time || defaultEnd,
/*'start_time': defaultStart,
'end_time': defaultEnd,*/
'color': pTask.color
}, {
headers: {
"Content-Type": "text/plain"
}
})
.success(function(data, status, headers, config) {
console.log(data);
TaskService.taskList.push(data);//pushing the new task
console.log("YYYYYYYYYYYYY -------->>>>>", defaultStart, newClickDate, newEditId);
})
.error(function(data, status, headers, config) {
console.log("Failed to add the task to DB");
});
};
return TaskService;
})
//START CONTROLLER
angular.module('zazzleToolPlannerApp')
.controller('CalendarCtrl', function ($scope, $mdDialog, $http, $rootScope, $timeout, User, Auth, UserService, TaskService) {
$scope.isAdmin = Auth.isAdmin;
$scope.getCurrentUser = Auth.getCurrentUser;
$scope.activeUser = $scope.getCurrentUser()._id;
//console.log('???????????? logging active/current user id', $scope.activeUser);
$scope.newTask = {};
$scope.newTask.project_name = "";
$scope.newTask.project_type = "";
$scope.newTask.location = "";
$scope.newTask.estimate_time = "";
$scope.newTask.project_client_name = "";
$scope.newTask.url = "";
$scope.newTask.resource_link = "";
$scope.newTask.notes = "";
$scope.newTask.color = "";
//console.log('00000000000 ', $scope.newTask); //empty
$scope.tasks = TaskService.taskList;
$scope.addTaskWithService = function () {
//note that you can process the promise right here (because of the return $http in the service)
TaskService.addTask($scope.newTask)
.success(function(data){
//here you can process the data or format it or do whatever you want with it
console.log("Controller: the task has been added");
$scope.tasks = [];// EMPTY THE ARRAY
TaskService.getTasks();
$scope.updateGridDataAwesome();
$scope.updateGridData();
//console.log('Taskservice Controller ', $scope.updateGridDataAwesome);
})
.error(function(data){
//something went wrong
console.log("Controller: error in adding task");
});
}
});
//END CONTROLLER
在
TaskService.addTask
的succes()
回调中,您正在将一个空数组实例分配给$scope.tasks
,从而断开对TaskService.taskList
的引用。因此,调用TaskService.getTasks()
将不再对作用域变量产生任何影响。如果将服务命名为TaskService,为什么它是工厂而不是服务?TaskService.getTasks()做什么?它是异步的吗?那么$scope.updateGridDataAwesome()和$scope.updateGridData()呢?clickDate和editId在代码中来自何处?@LionC:很抱歉,这不是服务,而是工厂1。阅读2。重构代码(使用代码样式指南)3。做一个傻瓜。主要错误是您使用了2个成功函数,并删除了$scope.tasks对TaskService.tasklist的引用。可能还有其他问题,TaskService.getTasks()
我认为是一个异步函数,因此如果调用后的代码假设数据已准备好,则应该在成功回调中执行。工厂成功函数中的回调?我指的是http调用的成功函数TaskService.getTasks()
正在(可能)进行。我建议按照@evc在评论中写的内容来做。