angularjs ngResource更新一个实体列表,然后更新另一个实体列表

angularjs ngResource更新一个实体列表,然后更新另一个实体列表,angularjs,node.js,ngresource,Angularjs,Node.js,Ngresource,我有两种资源:项目及其任务。 当用户需要更新项目时,他们可以更新标题,也可以更新其任务 如果用户创建了一个任务,则需要先将其保存到db以获取id,然后我可以将id分配给项目的tasks属性 因此,需要在保存所有任务后保存项目 $scope.update = function() { //each taskTemplate can be updated concurrently, or at the same time. they do not need to updat

我有两种资源:项目及其任务。 当用户需要更新项目时,他们可以更新标题,也可以更新其任务

如果用户创建了一个任务,则需要先将其保存到db以获取id,然后我可以将id分配给项目的tasks属性

因此,需要在保存所有任务后保存项目

    $scope.update = function() {

        //each taskTemplate can be updated concurrently, or at the same time. they do not need to updated one by one
        for (var taskTemplates in $scope.taskTemplates) {
            //i need to update all the tasks, and then I can update the project
        }

        //this needs to be update after all the taskTemplates are updated
        $scope.projectTemplate.$update(function() {
            $location.path('projectTemplate/' + $scope.projectTemplate._id)
        }, function(errResponse) {
            $scope.error = errorResponse.data.message
        })

    }

我如何实现这种订购

将任务承诺与
all()
组合,并使用
all(…)保存项目。然后(…)
。详情如下:

$scope.update = function() {
    var promises = [];
    for (var taskTemplates in $scope.taskTemplates) {
        promises.push(taskResource.save(taskTemplates).$promise);
    }
    $q.all(promises).then(function() {
        return projectResource.save(currentProject).$promise;
    });
}