In angular2, I create the observable _uploadProgressStatus, which takes in numbers. Using async pipe, I want to display the number on the UI and updated automatically when a new values is kicked in. Pls refer to the codes below.
home.component.ts is observing the data from upload.service.ts
public _uploadProgressStatus:Observable<number>;
this._uploadProgressStatus = this._uploadService._progress$;
home.component.html
<div *ngIf="_startUploadFlag" class="container">
<h4>Uploading...{{_uploadProgressStatus | async | percent}}</h4>
</div>
upload.service.ts
uploadVideo(file:File):Promise<Video> {
return new Promise((resolve, reject) => {
let formData: FormData = new FormData(),
authenCode = this._authenservice.authenticate(),
xhr: XMLHttpRequest = new XMLHttpRequest();
console.log('getVideos:authenCode is' + authenCode);
let path = this._configurationService.getAPI('API_VIDEOMANAGEMENT_UPLOAD');
console.log('upload video to:' + path);
xhr.open('POST', path, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log('xhr status 200');
resolve(<Video>JSON.parse(xhr.response));
} else {
console.log('xhr status rejected:'+xhr.status);
reject(xhr.response);
}
}
};
xhr.upload.onprogress = (event) => {
this._progress = Math.round(event.loaded / event.total * 100);
console.log('inside of xhr.upload.onprogress:'+ this._progress);
this._progressObserver.next(this._progress/100);
};
formData.append("file", file, file.name);
xhr.send(formData);
});
}
The problem I encounter is numbers come in without a problem, but UI is not updated. However, whenever I click on the <h4>
, the correct values is displayed. Just not automatically updated there. Any idea?