如何以角度从可观察/http/异步调用返回响应?
我有一个服务,它返回一个可观察量,它对我的服务器执行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!
}
}
我选中了如何从异步调用返回响应?帖子,但找不到解决方案