【发布时间】:2018-08-05 14:28:12
【问题描述】:
我目前正在尝试使用 *ngFor 遍历数组,但页面上没有显示任何内容。这是 HTML 的代码
<ion-header>
<ion-navbar>
<ion-title>
Recent News
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-card *ngFor="let article of articles">
<ion-card-content>
<h2>{{article.title}}</h2>
<p>{{article.description}}</p>
</ion-card-content>
</ion-card>
</ion-content>
<ion-footer>
<ion-toolbar>
<p>Powered by NEWS API</p>
</ion-toolbar>
</ion-footer>
以下是 .ts 中的代码(打字稿?)
getNews() {
this.rest.getNews()
.then(result => {
this.news = result;
console.log(this.news);
});
}
this.news 的控制台日志为this。但是,如果我console.log(this.news.articles[0]),this is the output instead。
是否可以这样我从 0 迭代到 9 而不是使用 ngFor 或者我错过了什么?
编辑: home.ts如下
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { RestProvider } from '../../providers/rest/rest';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
news: any;
errorMessage: string;
constructor(public navCtrl: NavController, public rest:RestProvider) {
this.getNews();
}
getNews() {
this.rest.getNews()
.then(result => {
this.news = result;
console.log(this.news.articles[0]);
});
}
}
【问题讨论】: