我有一个包含人员 FormArray 的表单。people 数组然后包含地址的 FormArray,它给了我以下 JSON:
{
"people": [
{
"lastName": "Last-1",
"firstName": "First-1",
"middleName": "",
"addresses": [
{
"street": "Person1 - Address1"
},
{
"street": "Person1 - Address2"
}
]
},
{
"lastName": "Last-2",
"firstName": "Last-2",
"middleName": "",
"addresses": [
{
"street": "Person2 - Address1"
}
]
}
]
}
现在我正在尝试从表单中获取这些“人”并创建一个 Person 对象数组
export class ReportPerson {
lastName: string = '';
firstName: string = '';
middleName: string = '';
addresses: PersonAddress[];
}
export class PersonAddress {
street: string = '';
}
当我使用 console.log(form.get('people').value); 我得到以下结果:
(2) [{…}, {…}]0: {lastName: "Last-1", firstName: "First-1", middleName: "",
addresses: Array(2)}
1: {lastName: "Last-2", firstName: "Last-2", middleName: "",
addresses: Array(1)}length: 2__proto__: Array(0)
但是无论我如何尝试获取数据,它都说我的列表是未定义的。例如,以下返回它无法读取未定义的“lastName”。
save(form: any) {
var reportPersonList: ReportPerson[] = new Array();
var people = form.get('people');
for (let i = 0; i < people.length; i++) {
console.log(people[i].lastName);
}
}
}
我的问题是,从 FormArray 中的数据创建人员对象数组的正确语法是什么?我知道这是我缺少的一些基本内容,但我已经习惯了 C# 并且对 Typescript/Angular2 很陌生。