【发布时间】:2016-08-13 03:03:43
【问题描述】:
我今天整天都在处理这个问题,似乎无法弄清楚我做错了什么。我敢肯定,我忽略了一些简单的事情,但我们将不胜感激。
所以问题来了。我有一个简单的组件,它应该使用 *ngFor 呈现应用程序 ID 列表。此列表是从 Web api 检索的。 http 请求按预期工作,我在网络上看到了正确的响应。
返回结果后,我将数组分配给组件中的一个局部变量,并且我假设 *ngFor 会识别更改并适当地绘制屏幕。
我在浏览器控制台中没有看到任何客户端错误。这也是 Angular 2 RC5。
应用程序列表.component.html
<h3>Applications</h3>
<ul>
<li *ngFor="let app of applications">{{ app.id }}</li>
</ul>
app-list.component.ts
import { Component, OnInit } from '@angular/core';
import { Application } from './application';
import { ApplicationService } from './application.service';
@Component({
selector: 'sng-application-list',
template: require('./application-list.component.html'),
providers: [ ApplicationService ]
})
export class ApplicationListComponent {
errorMessage: string;
applications: Application[] = [];
constructor(private applicationService: ApplicationService) { }
ngOnInit() {
this.getApplications();
}
getApplications() {
this.applicationService.getApplications()
.subscribe(
applications => {
this.applications = applications
},
error => {
this.errorMessage = <any>error;
});
}
}
【问题讨论】:
-
浏览器没有错误?我看到你没有导入应用程序,但你已经定义了:应用程序:应用程序 [] = [];
-
那是复制粘贴错误。
-
{{ app?.id }}试试这个。在 *ngFor 中尝试{{applications|json}}并告诉我你有没有数据? -
还是什么都没有。我可以看到数据到达并在订阅的成功处理程序中设置,但它的变化检测从未启动。在我执行修改应用程序数组的后续操作后,列表将呈现。
标签: angular