、Observableを別のObservableに割り当てる理由は、後者はデータを受け取りません。 angular2で
update.service.ts別のコンポーネントhome.component.ts
private _uploadProgressStatus:Observable<number>;
constructor(private _uploadService:UploadService) {
this._uploadProgressStatus = this._uploadService._progress$;
this._uploadProgressStatus.subscribe(
data => {
console.log('progress = '+data/100);
}); //if subscribe to this._uploadProgressStatus, no values are received...
this._uploadService._progress$.subscribe(
data => {
console.log('progress = '+data/100);
});
} // if subscribe to this._uploadService._progress$ ,numbers are received.
で
public _progress$: Observable<number>;
private _progress: number = 0;
private _progressObserver: Observer<number>;
constructor(private _http:Http, private _configurationService: ConfigurationService,private _authenservice:AuthenticationService) {
this._progress$ = new Observable(observer => {
this._progressObserver = observer
});
}
....
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);
this._progressObserver.next(this._progress);
};
formData.append("file", file, file.name);
xhr.send(formData);
});
}
原因が何であるかを任意のアイデア?
両方のHTTPリクエストからの応答が同時に到着しなければならないか、彼らは完全に異なる要求されているのですか? – echonax
こんにちは、私は詳細でコードを更新します。 Tks – Hammer
'this._uploadProgressStatus = this._uploadService._progress $'です。つまり、同じhttpリクエストを2回連続して作成していますか? ja – echonax