Angular onreadystatechange

Angular onreadystatechange

本文关键字:onreadystatechange Angular      更新时间:2023-09-26

Angular 新手在这里。我想知道是否可以使用 $http 服务在任何就绪状态/状态更改时调用函数,而不仅仅是在成功/失败时调用函数。就代码而言,它将是以下JS代码的等效角度代码:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function() {
    if (xhr.readyState < 4 && xhr.status != 200)
        alert('Loading');
}
xhr.open...
xhr.send...

该事件不再在 1.3+ 中使用,也不会公开,仅在 1.2 中使用状态 4。

做这样的事情的唯一方法(如果你真的想的话)就是更换或装饰$httpBackend......

在 http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.js 查看createHttpBackend()

我成功使用的另一种方法是在$http调用之后调用 $scope.$apply():

// using $http
var promise = $http.get(url).then(success, ...); 
// without $http
$.ajax({
     url: url,
     success: function() {
         success();
         $scope.$apply(); // if you'd used $http, this would be unnecessary
     },
     ...
});
相关文章:
  • 没有找到相关文章