【发布时间】:2019-05-06 07:31:12
【问题描述】:
我有一个 *ngFor 指令,它按预期运行并在我的浏览器中显示数据。尽管我的 chrome 控制台显示“未定义”错误。
http.service.ts:
getExerciseProgress(exId: number): Observable<Exercise> {
return this.http.get<Exercise>(this.baseURL + 'exercises/GetProgressionByExerciseId/' + exId)
}
view-exercise.component.ts:
exercise: Exercise;
constructor(private http: HttpService) { }
ngOnInit() {
this.http.getExerciseProgress(7).subscribe(ex =>{
this.exercise = ex;
console.log(this.exercise);
});
}
作为参数传递的 7 用于测试目的,当我记录结果时,该对象似乎就是我正在寻找的。一个带有嵌套 Progress 数组的练习对象。
view-exercise.component.html:
<p *ngFor="let p of exercise.progress">{{ p.bpm }}</p>
以上行是在我的 Chrome 控制台窗口中抛出以下消息的行。 “错误类型错误:无法读取未定义的属性‘进度’”。尽管如此,我的浏览器仍显示正确的信息。
客户端模型(以防万一):
export class Exercise {
id: number;
description: string;
progress: Progress[];
constructor(id: number, description: string, progress: Progress[]){
this.id = id;
this.description = description;
this.progress = progress;
}
export class Progress {
id: number;
dateAttempted: Date;
bpm: number;
constructor(id: number, dateAttempted: Date, bpm: number){
this.id = id;
this.dateAttempted = dateAttempted;
this.bpm = bpm;
}
}
提前致谢
【问题讨论】:
标签: angular typescript ngfor