【发布时间】:2016-03-22 18:39:58
【问题描述】:
我正在学习 Angular 2,我一直在使用静态数组,现在我正在尝试了解有关读取远程数据的更多信息。
我的app.component.ts:
import {Component} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'my-app',
template:`
<h1>Angular2 HTTP Demo App</h1>
<h2>Foods</h2>
<ul>
<li *ngFor="#doc of docs">{{doc.id}}</li>
</ul>
`
})
export class AppComponent {
public foods;
public books;
public movies;
constructor(private http: Http) { }
ngOnInit() {
this.getFoods();
}
getFoods() {
this.http.get('http://my API URL')
.map((res:Response) => res.json())
.subscribe(
data => { this.foods = data},
err => console.error(err),
() => console.log('done')
);
}
}
这是我的 API url 显示结果的方式:
- 一个名为“docs”的 json 对象。
- 带有 id 和其他字符串的项目的 json 数组。
我的目标是循环每个数组项并显示其中的数据(id、placeID 等)
这是我的应用程序,它根本没有迭代:
如何循环使用 *ngFor 每个 json 数组项?
【问题讨论】:
-
docs()是做什么的? -
没什么,这只是我发现并尝试使用它的一些技巧。