Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use eventHandlers and uploadEvntHandlers with angularjs 1 http requests?

I tried this code with no success

$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);
});

I checked here :

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

and there :

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

It is just that I would like to improve my code with a progress bar on a big json file download to the app.

By the way, I could not find a way to log the overall $http object's functions and supported events as it returns a promise object with few info.

like image 958
Mantisse Avatar asked Jun 26 '16 10:06

Mantisse


1 Answers

Don't use onprogress, use just progress, same with other events. I prepare a plunkr to demonstrate:

  $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);
    });

Also, there was a bug that has been fixed in angular 1.5.5 as it can be seen from the CHANGELOG. Update to the 1.5.5 or higher and it will work.

like image 180
ganqqwerty Avatar answered Oct 21 '22 09:10

ganqqwerty