我被困在这里几个小时,需要帮助。如何进行http请求,以便在加载时可以使用它。目前使用以下代码,视图会加载,但服务器的响应不会到来,必须等待几秒才能使其可用。
的src /应用程序/ example.ts
import { MyApp } from 'app.component';
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class ExampleApis{
http: any;
requestURL: string;
constructor( http:Http ){
this.http = http;
this.requestURL = 'https://www.example.com/api';
}
locations(){
return this.http.get( this.requestURL + '/locations/?format=json').map( result => result.json() );
}
}
的src /页/地方对入住/地方对stay.ts 的
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { ExampleApis } from '../../app/apis';
@Component({
selector: 'page-places-to-stay',
templateUrl: 'places-to-stay.html'
})
export class PlacesToStayPage {
locationsList: any;
constructor(public navCtrl: NavController, public navParams: NavParams, public ExampleApis:ExampleApis) {
this.regions();
}
locations(){
this.ExampleApis.locations().subscribe( response => {
this.locationsList = response.results;
});
}
}