【发布时间】:2019-04-03 22:36:44
【问题描述】:
我有这段代码可以读取一些 json 数据:
import {Component} from 'angular2/core';
import {Http, Response} from 'angular2/http';
@Component({
selector: 'my-app',
template: `
<h2>Basic Request</h2>
<button type="button" (click)="makeRequest()">Make Request</button>
<div *ngIf="loading">loading...</div>
<pre>{{data | json}}</pre>
`
})
export class AppComponent {
data: Object;
loading: boolean;
constructor(public http: Http) {
}
makeRequest(): void {
this.loading = true;
this.http.request('http://jsonplaceholder.typicode.com/photos/1')
.subscribe((res: Response) => {
this.data = res.json();
this.loading = false;
}); }
}
This is the returned json:
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/30ac17"
}
{{data | json}} 正在返回所有数据。
例如,我只想获得标题。 所以我尝试了这个:
{{data.title | json}} 但这不起作用。
只显示标题的正确方法是什么?
【问题讨论】:
标签: javascript angular