我尝试在此处显示来自 API 的数据,但在控制台输出中收到错误“”(见图)
我的问题是,我不知道为什么会收到错误以及我能做什么,我已经尝试过不使用 * 并且使用不同的符号。 CommonModuels 和 NgModuels 模块也导入到 API 服务和 thets 中。
<ion-list>
<ion-list-header>
Stundet List
</ion-list-header>
<ion-item lines="insert" *ngFor="let student for students">
<ion-label>
<p> {{ students.studentsOne }} </p>
</ion-label>
</ion-item>
</ion-list>
import { Component } from '@angular/core';
import { ApiService } from '../api.service';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
imports: [CommonModule, NgModule]
})
export class HomePage {
year: any;
studentOne: any;
studentTwo: any;
student: any[] = [];
constructor(public _apiService: ApiService)
{
this.getStudents()
}
addStudent(){
let data = {
year: this.year,
studentOne: this.studentOne,
studentTwo: this.studentTwo,
}
this._apiService.addStudent(data).subscribe((res:any) => {
console.log("SUCCESS ===",res);
this.year = '';
this.studentOne = '';
this.studentTwo = '';
alert('SUCCESS')
this.getStudents()
},(error: any) => {
console.log("ERROR ===",error);
alert('ERROR');
})
}
getStudents(){
this._apiService.getStudents().subscribe((res:any) => {
console.log("SUCCESS ===",res);
this.student = res;
},(error: any) => {
console.log("ERROR ===",error);
})
}
}
使用
of
代替 for
<ion-item lines="insert" *ngFor="let student for students">
到
<ion-item lines="insert" *ngFor="let student of students">