我确实有这个带有Angular Material属性的HTML:
<div formArrayName="emails">
<div *ngFor="let email of emails.controls; index as i" [formGroupName]="i">
<mat-input-container>
<input matInput formControlName=i placeholder="Email- Address">
</mat-input-container>
</div>
</div>
输入字段中未显示 email.value 的值,而formControlName = i不起作用。 有谁知道我要做什么才能在输入字段中显示电子邮件地址?
电子邮件看起来像这样:
this.form = this.formBuilder.group({
emails: this.formBuilder.array([]),
...
});
并在从后端或可观察的数据中获取数据之后填充:
for(let i = 0; i < this.contact.emails.length; i++) {
this.addEmail();
}
addEmail() {
let email = new FormControl('');
let emails: FormArray = this.form.controls['emails'] as FormArray;
emails.push(email);
}
let emails: FormArray = this.form.controls['emails'] as FormArray;
for(let i = 0; i < this.contact.emails.length; i++) {
emails.controls[i+''].patchValue(this.contact.emails[i]);
}
答案 0 :(得分:0)
addEmail() {
const controls = <FormArray>this.form.controls['emails'];
for(let i = 0; i < this.contact.emails.length; i++) {
let email = this.formBuilder.group({
email: [{value: this.contact.emails[i], disabled: false}, [Validators.required]]
});
controls.push(email);
}
}