我有这样的代码,其读取一些JSON数据:Angular2 HTTP显示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}}
但这是行不通的。
显示标题的正确方法是什么?
这并不返回任何东西...编辑说:未解决的变量标题 – Satch3000
我有同样的问题。只需在html代码中写入对象,就会在视图中打印“[object Object]”。尝试获取“异常:TypeError:l_fav0在[null]”中未定义的任何属性结果,其中“fav”是我的对象的名称。我不知道我错过了什么,但它与Satch3000的问题相同。整个网络只给出相同的建议,没有帮助,没有例子,除了太简单的例子。 –