显示从 api 获取的数据
Display fetched data from api
我已经从 API 获取了数据,我正试图将它们显示为一个数组。
我能够记录请求数据并将它们记录到控制台中。
我无法将它们显示到数组中。
这是我的服务组件:
import {Injectable} from '@angular/core';
import {HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class EarthquakeService {
constructor(private http: HttpClient) {}
getEarthquakes(){
return this.http.get('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson');
}
}
这是我请求数据的应用程序组件:
import { Component, OnInit } from '@angular/core';
import {EarthquakeService} from '../../services/earthquake/earthquake.service';
@Component({
selector: 'app-earthquake-page',
templateUrl: './earthquake-page.component.html',
styleUrls: ['./earthquake-page.component.scss']
})
export class EarthquakePageComponent implements OnInit {
earthquakes: string[];
eathqaukeTitle: string;
constructor(private earthquakeService: EarthquakeService) {
}
ngOnInit() {
this.getEarthqaukes();
}
getEarthqaukes(){
this.earthquakeService.getEarthquakes().subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
}
现在我不知道如何将这些数据保存到数组中并显示在页面上。
我认为您在发出 http
获取数据的请求时遗漏了一些部分,请尝试以下操作。该请求应该类似于以下内容,无法从图片中很好地计算出 JSON。
请求
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
public getEarthquakes(): Observable<any>{
return this.http.get('http://earthquake.usgs.gov/
earthquakes/feed/v1.0/summary/all_hour.geojson').pipe(map(data => data));
}
//only on two lines for SO readability
组件
public earthQuakes = [];
getEarthqaukes(){
this.earthquakeService.getEarthquakes().subscribe(
(response) => {
console.log(response)
this.earthQuakes = response.features; // whichever bit you are looking for.
},
(error) => console.log(error)
);
}
HTML
<div *ngFor="let shake of earthQuakes">
<span>{{shake}}</span>
</div>
Documentation on using *ngFor.
如前所述,这并不完美 JSON,但这绝对足以填补空白。正如 the head rush you should look through this tutorial 在评论中提到的那样,以确保您在学习时了解自己在做什么。
我已经从 API 获取了数据,我正试图将它们显示为一个数组。 我能够记录请求数据并将它们记录到控制台中。
我无法将它们显示到数组中。
这是我的服务组件:
import {Injectable} from '@angular/core';
import {HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class EarthquakeService {
constructor(private http: HttpClient) {}
getEarthquakes(){
return this.http.get('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson');
}
}
这是我请求数据的应用程序组件:
import { Component, OnInit } from '@angular/core';
import {EarthquakeService} from '../../services/earthquake/earthquake.service';
@Component({
selector: 'app-earthquake-page',
templateUrl: './earthquake-page.component.html',
styleUrls: ['./earthquake-page.component.scss']
})
export class EarthquakePageComponent implements OnInit {
earthquakes: string[];
eathqaukeTitle: string;
constructor(private earthquakeService: EarthquakeService) {
}
ngOnInit() {
this.getEarthqaukes();
}
getEarthqaukes(){
this.earthquakeService.getEarthquakes().subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
}
现在我不知道如何将这些数据保存到数组中并显示在页面上。
我认为您在发出 http
获取数据的请求时遗漏了一些部分,请尝试以下操作。该请求应该类似于以下内容,无法从图片中很好地计算出 JSON。
请求
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
public getEarthquakes(): Observable<any>{
return this.http.get('http://earthquake.usgs.gov/
earthquakes/feed/v1.0/summary/all_hour.geojson').pipe(map(data => data));
}
//only on two lines for SO readability
组件
public earthQuakes = [];
getEarthqaukes(){
this.earthquakeService.getEarthquakes().subscribe(
(response) => {
console.log(response)
this.earthQuakes = response.features; // whichever bit you are looking for.
},
(error) => console.log(error)
);
}
HTML
<div *ngFor="let shake of earthQuakes">
<span>{{shake}}</span>
</div>
Documentation on using *ngFor.
如前所述,这并不完美 JSON,但这绝对足以填补空白。正如 the head rush you should look through this tutorial 在评论中提到的那样,以确保您在学习时了解自己在做什么。