Using Angular 1.5.7 I am using the uploadEventHandlers
to get progress. It works in the factory but the values don't update in the controller.
Factory:
.factory("PostFactory", function($http,baseUrl) {
var service = {};
service.progressBar = 0;
service.progressCounter = 0;
service.createPost = function(payload) {
return $http({
data: payload,
url: baseUrl + 'api/post',
method: 'POST',
uploadEventHandlers: {
progress: function (e) {
if (e.lengthComputable) {
service.progressBar = (e.loaded / e.total) * 100;
service.progressCounter = service.progressBar;
console.error("Inside factory: " + service.progressCounter);
}
}
}
}).then(function(response){
return response.data;
});
return service;
});
}
Controller:
.controller('SendPostCtrl', function($scope,$timeout,PostFactory) {
var post = {
// all my data (including a base64 encoded image) to send to the server
}
$scope.uploadProgress = PostFactory.progressCounter;
$scope.$watch($scope.uploadProgress, function() {
console.error("Inside controller: " + $scope.uploadProgress);
});
var promise = PostFactory.createPost(post);
promise.then(function(response) {
//do stuff after the server response with success data
});
});
As shown, the console will is:
> Inside controller: 0
> Inside factory: 13.1
> Inside factory: 26.3
> Inside factory: 39.4
> Inside factory: 52.6
> Inside factory: 65.8
> Inside factory: 78.9
> Inside factory: 92.1
> Inside factory: 100
How do I get realtime updates on the progress within the controller?