簡體   English   中英

角度2:使用Observable並返回Observable

[英]Angular 2: Work with Observable and return Observable

我對angular2和observables有疑問。 在服務中,我想使用可觀察對象(從另一服務加載),然后也將數據作為可觀察對象返回。

我怎樣才能做到這一點? 我有以下代碼:

getEPGDayByChannel(channelID, newDate) {
        let mydate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate(), 0, 0, 0);


            let fromDate = new Date(mydate.getFullYear(), mydate.getMonth(), mydate.getDate(), 0, 0, 0);
            let endDate = new Date(mydate.getFullYear(), mydate.getMonth(), mydate.getDate(), 23, 59, 59);
            this.apiService.getChannelEPGbyTime(channelID, fromDate, endDate).
            subscribe(
            data => {
                //do some magic with the data
                // return some thing of the data as an observable
                return Observable.of(data.programme);
            },
            error => { this.variables.setFailure(error);}
            );

     }

但是用此代碼我得到了這個失敗:

EXCEPTION: Uncaught (in promise): TypeError: this.epgService.getEPGDayByChannel(...) is undefined

Unhandled Promise rejection: this.epgService.getEPGDayByChannel(...) is undefined ; Zone: angular ; Task: Promise.then ; Value: TypeError: this.epgService.getEPGDayByChannel(...) is undefined

我會很高興獲得一些幫助!

嘗試這個:

getEPGDayByChannel(channelID, newDate) {
    let mydate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate(), 0, 0, 0);


    let fromDate = new Date(mydate.getFullYear(), mydate.getMonth(), mydate.getDate(), 0, 0, 0);
    let endDate = new Date(mydate.getFullYear(), mydate.getMonth(), mydate.getDate(), 23, 59, 59);

    return this.apiService.getChannelEPGbyTime(channelID, fromDate, endDate)
        .map(data => {
            //do some magic with the data
            // return some thing of the data as an observable
            return data.programme;
        })
        .catch(error => { 
            this.variables.setFailure(error);
        });

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM