将结果强制转换为get方法中的Typescript类

时间:2018-08-27 07:43:46

标签: angular typescript ionic-framework ionic2 ionic3

我是离子新手。我试图将响应从我的API转换为Typescript类。 我正在用离子技术做到这一点。

我的打字稿课:

export class StudentDetailsModel {
  StudentId: number
  StudentName: string
  IsPresent: boolean = true
}

我对API的回应:

{
  StudentId: 10,
  StudentName: "Farooq  Zahid",
  StudentGender: null,
  StudentDateofBirth: "0001-01-01T00:00:00",
  BirthPlace: null,
  …
}

我的获取方法:

studentsList: StudentDetailsModel[];
getStudentsList(selected: AttendanceCreateModel) {
  return this.http.get('http://localhost:58158/api/Academicyear/GetStudentsList?batchId=' + selected.BatchId)
    .subscribe(
      (data: StudentDetailsModel[]) => {
        this.studentsList = data;
        console.log(this.studentsList);
      });
}

当我执行console.log(this.studentsList)时,我期望的是

{
  StudentId: 10,
  StudentName: "Farooq  Zahid",
  IsPresent: true
}

现在我得到的是

{
  StudentId: 10,
  StudentName: "Farooq  Zahid",
  StudentGender: null,
  StudentDateofBirth: "0001-01-01T00:00:00",
  BirthPlace: null,
  …
}

我也尝试过,但是没有得到预期的结果。

return this.http.get < StudentDetailsModel[] > ('http://localhost:58158/api/Academicyear/GetStudentsList?batchId=' + selected.BatchId)
  .subscribe(
    data => {
      this.studentsList = data;
      console.log(this.studentsList);
    });

0 个答案:

没有答案