【问题标题】:How to use eventHandlers and uploadEvntHandlers with angularjs 1 http requests?如何将 eventHandlers 和 uploadEvntHandlers 与 angularjs 1 http 请求一起使用?
【发布时间】:2016-10-28 11:37:43
【问题描述】:

我试过这段代码没有成功

$http({
    method:"GET",
    url:"data/mycooljsonfile.json",
    eventHandlers:{
        onprogress:function(event){
        console.log("progress");
        console.log(event);
    },onreadystatechange:function(event){
        console.log("change");
        console.log(event);
    }
    },
    uploadEventHandlers:{
         onprogress:function(object){
              console.log(object);
         }
    }
})
.success(function(json){ // succès
     $scope.lemmes=json;
      //console.log($http);
}).error(function(error){ // erreur
    console.log(error);
});

我在这里检查过:

https://docs.angularjs.org/api/ng/service/$http

还有:

https://www.w3.org/TR/XMLHttpRequest/#events

只是我想改进我的代码,在将大 json 文件下载到应用程序时使用进度条。

顺便说一句,我找不到记录整个 $http 对象的功能和支持的事件的方法,因为它返回了一个包含少量信息的 promise 对象。

【问题讨论】:

  • 我知道可以用纯 javascript 来做,我的问题是关于使用 Angular JS 版本 1,它在标题中。
  • 现在在这个问题上摸索了几天。没有触发事件:(
  • 德拉特。找到了解决方案。更新到 angularjs 1.5.5 :D
  • 太好了,如果可以的话,希望我能参与到文档中。 @SSR 我可以有一个用例的链接吗?

标签: angularjs http xmlhttprequest


【解决方案1】:

不要使用onprogress,只使用progress,与其他事件相同。我准备了一个plunkr 来演示:

  $http({
      method: "GET",
      url: "data.json",
      eventHandlers: {
        progress: function(event) {
          console.log("progress");
          console.log(event);
        },
        readystatechange: function(event) {
          console.log("change");
          console.log(event);
        }
      },
      uploadEventHandlers: {
        progress: function(object) {
          console.log(object);
        }
      }
    })
    .success(function(json) { // succès
      $scope.lemmes = json;
      //console.log($http);
    }).error(function(error) { // erreur
      console.log(error);
    });

此外,从CHANGELOG 可以看出,在 angular 1.5.5 中已经修复了一个错误。更新到 1.5.5 或更高版本就可以了。

【讨论】:

  • 感谢您的回答,我会在空闲时间尝试一下。干杯。
  • @mantisse 用绿色勾号接受它,我会得到一些业力
猜你喜欢
  • 1970-01-01
  • 2017-09-10
  • 2023-03-14
  • 2018-10-17
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 2016-11-08
  • 2016-09-01
相关资源
最近更新 更多