2016-05-05 8 views
1

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

原因が何であるかを任意のアイデア?

+0

両方のHTTPリクエストからの応答が同時に到着しなければならないか、彼らは完全に異なる要求されているのですか? – echonax

+0

こんにちは、私は詳細でコードを更新します。 Tks – Hammer

+1

'this._uploadProgressStatus = this._uploadService._progress $'です。つまり、同じhttpリクエストを2回連続して作成していますか? ja – echonax

答えて

0

は、オペレータが、インポートする必要があり、観察.share()

this._progress$ = new Observable(observer => { 
     this._progressObserver = observer 
    }).share(); 

を作ります。

import 'rxjs' 

または

import 'rxjs/add/operator/share' 
+0

Tks、ガンター、それは良い作品です。したがって、jsの割り当ては、既存のobservableへの参照を作成し、複数のobserverablesとshare()を使用する必要があります。 – Hammer

+0

うん、コピーは同じ観測可能性を指します。複数の加入者には、それを共有する必要があります。 –

関連する問題