0

我试图在 saveInterview 完成后执行的 Angular 控制器中获取以下 findTimelineEntries 函数:

$scope.saveInterview = function() {
        $scope.interviewForm.$save({employeeId: $scope.employeeId}, function() {
            $scope.findTimelineEntries();
        });
    };

保存操作添加或编辑也是时间线条目的一部分的数据,因此我希望显示更新的时间线条目。首先,我尝试将其更改为:

$scope.saveInterview = function() {
        var functionReturned = $scope.interviewForm.$save({employeeId: $scope.employeeId});
        if (functionReturned) {
            $scope.findTimelineEntries();
        }
    };

后来到这个:

$scope.saveInterview = function() {
        $scope.interviewForm.$save({employeeId: $scope.employeeId});
    };

    $scope.saveInterview.done(function(result) {
        $scope.findTimelineEntries();
    });

最后我找到了一些关于承诺的信息,所以我尝试了这个:

    $scope.saveInterview = function() {
        $scope.interviewForm.$save({employeeId: $scope.employeeId});
    };

    var promise = $scope.saveInterview();
    promise.done(function() {
        $scope.findTimelineEntries();
    });

但不知何故,根据http://nurkiewicz.blogspot.nl/2013/03/promises-and-deferred-objects-in-jquery.html它确实以这种方式工作的事实并不意味着我可以使用相同的那些 $scope.someFuntcion = function() 函数的方法:-S

4

1 回答 1

1

这是一个使用 Promise 的示例。首先,您需要将 $q 包含到您的控制器中。

$scope.saveInterview = function() {
   var d = $q.defer();
   // do something that probably has a callback. 
   $scope.interviewForm.$save({employeeId: $scope.employeeId}).then(function(data) {
      d.resolve(data);  // assuming data is something you want to return. It could be true or anything you want.
   });


   return d.promise;

}
于 2013-05-21T14:15:21.137 回答