Angular 角度2:使用可观察和返回可观察

Angular 角度2:使用可观察和返回可观察,angular,observable,Angular,Observable,我有一个问题要问angular2和observables。 在一个服务中,我希望使用一个可观察对象(从另一个服务加载),然后以可观察对象的形式返回数据 我该怎么做? 我有以下代码: getEPGDayByChannel(channelID, newDate) { let mydate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate(), 0, 0, 0); le

我有一个问题要问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);
        });

 }
试试这个:

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

 }

真的,这么简单??:D一切都好,非常感谢!花了一整天的时间在这上面谢谢@朱尼亚斯是的,就是这么简单。但是您需要了解,
apiService.getChannelEPGbyTime
不会在您调用
getepgdaychannel
方法时立即被调用,而只有在您订阅它时才会被调用。您的代码确实通过调用API服务来调用它,但没有订阅。真的,这么简单??:D一切都好,非常感谢!花了一整天的时间在这上面谢谢@朱尼亚斯是的,就是这么简单。但是您需要了解,
apiService.getChannelEPGbyTime
不会在您调用
getepgdaychannel
方法时立即被调用,而只有在您订阅它时才会被调用。您的代码只是通过调用API服务而没有订阅它。