AngularJS - 我不明白`factory`是如何工作的

时间:2015-08-13 07:48:16

标签: angularjs

我得到了这项服务,我把所有关于检索,删除和添加任务的http请求都放在了这里。删除和检索任务可以正常工作,但是当我添加任务或更新现有任务时,任务更改将无法直观显示。我必须刷新浏览器。

我只是不明白我怎么似乎无法将我的任务添加到工作中。如果有人可以帮我解决我的代码,我将非常感激。

我制作了一个GIF,在这个示例中我演示了我的问题,我更新了一个任务 (https://www.dropbox.com/s/qndkfmxde6fos9r/hiI1hBNvSn.gif?dl=0

我app.js代码的一部分:

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

1 个答案:

答案 0 :(得分:0)

succes()的{​​{1}}回调中,您将空数组实例分配给TaskService.addTask,从而中断对$scope.tasks的引用。因此,调用TaskService.taskList不再对范围变量产生任何影响。