angular - 我正在使用动态表单从表单中检索值,但是当我控制台记录我从该表单中的值创建的对象时,我得到了未定义
问题描述
@ViewChild('studentForm', {static: false}) studentForm: NgForm
students: Student[];
updatedStudent ={
Student_ID: 0,
Student_Name: '',
Student_Number: '',
Student_Surname: ''
}
onUpdateStudent(studentForm: NgForm){
this.editMode = false;
const updatedStudentValue = studentForm.value
console.log(updatedStudentValue)
const updatedStudent = new Student(
this.updatedStudent.Student_ID,
updatedStudentValue.updatedStudent.Student_Name,
updatedStudentValue.updatedStudent.Student_Number,
updatedStudentValue.updatedStudent.Student_Surname
)
console.log(this.updatedStudent)
}
当我控制台记录 updatedStudentValues 时,我得到了我的学生的一个对象,但是当我尝试为我的学生模型制作一个对象时,它显示未定义的 Student_Name 我该如何解决这个问题。
任何帮助将非常感激!
export class Student {
public Student_ID: number;
public Student_Name: string;
public Student_Number: number;
public Student_Surname: string;
constructor(
Student_ID: number,
Student_Name: string,
Student_Number: number,
Student_Surname: string
) {
this.Student_ID = Student_ID;
this.Student_Name = Student_Name;
this.Student_Number = Student_Number;
this.Student_Surname = Student_Surname;
}
}
感谢您对我是 Angular 新手的任何帮助。
解决方案
this.updated student 与 const updatedStudent 不同。您应该创建如下对象:
this.updatedStudent = new Student(
this.updatedStudent.Student_ID,
updatedStudentValue.updatedStudent.Student_Name,
updatedStudentValue.updatedStudent.Student_Number,
updatedStudentValue.updatedStudent.Student_Surname
)
console.log(this.updatedStudent)
使用 const updatedStudent,您正在创建一个新对象,它是块范围的并且与 this.updatedStudent 没有关系
推荐阅读
- highcharts - Highchart EMA、价格和 MACD 图
- ruby-on-rails - 第一次出现 Ruby 会话负载问题
- google-sheets - 谷歌查询语言:根据每列的条件计算多列并按日期分组
- android - How to align fab to the bottom right of screen inside a fragment in a recycler view?
- sample - Generating different samples of data columns
- outlook - Create Multible Outlook meetings in one ics File
- laravel - Laravel, jetstream global language prefix to routes
- asp.net-mvc - left join query in MVC
- excel - 在excel中找到位于底行的值
- node.js - 将多个投影与 MongoDB 聚合中的条件相结合