我有一个服务,它返回一个可观察的,它向我的服务器发出一个http请求并获取数据。我想使用这些数据,但最终总是得到undefined。有什么问题吗?
服务
@Injectable()
export class EventService {
constructor(private http: Http) { }
getEventList(): Observable<any>{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.get("http://localhost:9999/events/get", options)
.map((res)=> res.json())
.catch((err)=> err)
}
}组件:
@Component({...})
export class EventComponent {
myEvents: any;
constructor( private es: EventService ) { }
ngOnInit(){
this.es.getEventList()
.subscribe((response)=>{
this.myEvents = response;
});
console.log(this.myEvents); //This prints undefined!
}
}我查看了如何从异步调用返回响应?的帖子,但没有找到解决方案
https://stackoverflow.com/questions/43055706
复制相似问题