角度最终条款没有被称为承诺

时间:2015-09-07 06:58:18

标签: javascript angularjs angular-promise

我有一个RestService,它在调用请求时返回一个promise。这又从RestService中定义的方法调用,最后从控制器方法调用,如下所示。我假设在成功或失败的情况下会在控制器中调用finally子句,但它不会被调用。我使用的是角1.3.13。

"log"

1 个答案:

答案 0 :(得分:0)

我终于成功了。出于某种原因,如果我在$ http上设置了finally处理程序,则会调用链中的finally处理程序。即如果我有以下代码,它就会起作用:

var restService = {
'request': function(args) {
    var deferred = $q.defer();
        $http({
            url: url,
            withCredentials: this.use_session,
            method: method.toUpperCase(),
            headers: headers,
            params: params,
            data: data,
            timeout: 20000
        })
        .success(angular.bind(this, function(data, status, headers, config) {
            deferred.resolve(data, status);
        }))
        .error(angular.bind(this, function(data, status, headers, config) {
           deferred.reject(data);
        })).finally(function() {
           // Just by adding a finally clause on $http, the finally
           // handler in the chain i.e. in the controller utilizing
           // the restService, does get called.
        });
   return deferred.promise;
}

'listAssignments': function(filters) {
  return this.request({
        'method': "GET",
        'url': "/assignments/",
        'params': filters
      });
}

}; // end restService object

 restService.listAssignments(filters)
  .then(function(data) {
    $log.debug("success " + data);  
  }, function(data) {
    $log.debug("error " + data);  
  })
  .finally(function() {
    $log.debug("In finally.... calling scrollHelper");
  });