gpt4 book ai didi

Angular 6 FormArray 和 HTML 循环

转载 作者:行者123 更新时间:2023-12-02 00:23:01 25 4
gpt4 key购买 nike

全部,

我很难让 FormArray 与我当前的应用程序一起工作。我正在尝试从服务中获取姓名并将其添加到预订表中(并为用户提供添加/删除/修改姓名的能力)

相关代码如下:

reservationForm: FormGroup;
attendeeArray: FormArray;

constructor(
private groupMeetingService: GroupMeetingsService,
private formBuilder: FormBuilder
) {}

ngOnInit() {
this.initialize();
}

private createAttendeeName(name: string): FormControl {
return this.formBuilder.control({
name: name
});
}

private initialize(): void {
this.attendeeArray = this.formBuilder.array([]);
this.reservationForm = this.formBuilder.group({
attendeeRadio: ['inPerson'],
attendeeName: this.attendeeArray,
});

this.groupMeetingService.getMeeting(this.meeting.id).subscribe(meeting =>
{
this.meetingLocation = meeting.location;
});
this.groupMeetingService
.getReservations(this.meeting.id)
.subscribe(data => {
this.reservations = data;
this.attendeeArray = this.getAttendeeNames();
data.attendees.forEach(attendee => {
this.attendeeArray.push(this.createAttendeeName(attendee.name));
});
console.log('array', this.reservationForm);
});
}

当我查看我的控制台时,它确实显示它正在被添加。上面的代码是否正确?

如果它是正确的,我如何在 HTML 中循环它并让它显示出来?

我已经尝试了很多东西,但没有运气:-(

<div class="reservation-container" [formGroup]="reservationForm">
<div formArraryName="attendeeArray" *ngFor="let attendee of reservationForm.controls['attendeeName'].controls; let i = index">
<input [formControlName]="i" value="{{ attendee.name }}">
</div>
</div>

谢谢乔普夫

最佳答案

考虑以下问题

<div class="reservation-container" [formGroup]="reservationForm">
<div formArraryName="attendeeArray" *ngFor="let attendee of reservationForm.get('attendeeName')['controls']; let i = index">
<input [formControlName]="i" value="{{ attendee.name }}">
</div>
</div>

关于Angular 6 FormArray 和 HTML 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54788132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com