gpt4 book ai didi

javascript - Angular 嵌套 FormArray

转载 作者:行者123 更新时间:2023-12-03 00:11:30 27 4
gpt4 key购买 nike

我正在尝试使用以下方法创建具有嵌套 formArray 的动态表单,但收到此错误“无法在“Element”上执行“setAttribute”:“]”不是有效的属性名称。”

我不确定我哪里做错了,有人可以建议我吗?

TS:

createProductForm() {
this.productForm = this.formBuilder.group({
variations: this.formBuilder.array([this.createVariation()])
});
}

createVariation() {
return this.formBuilder.group({
variName: '',
variOptions: this.formBuilder.array([this.createOptions()])
});
}

addVariation(): void {
(this.productForm.controls['variations'] as FormArray).push(this.createVariation());
}

createOptions() {
return this.formBuilder.group({
optName: '',
optStock: '',
optSku: '',
optDisPrice: '',
optActPrice: ''
});
}

addOption(): void {
(this.productForm.controls['variOptions'] as FormArray).push(this.createOptions());
}

HTML:

<div class="card-block">
<button type="button" class="btn btn-primary" (click)="addVariation()">Add Variation</button>
<div class="card-text">

<div class="clr-row" formArrayName="variations"
*ngFor="let variation of productForm.controls['variations'].controls; let i = index;">
<div class="card" [formGroupName]="i">
<div class="card-block">
<clr-input-container>
<label>Variation Name</label>
<input clrInput type="text" formControlName="variName"/>
</clr-input-container>
<div formArrayName="variOptions"
*ngFor="let option of variation.controls['variOptions'].controls; let j = index;">
<div [formGroupName]="j"]>
<clr-input-container>
<label>Option Name</label>
<input clrInput type="text" formControlName="optName"/>
<button type="button" class="btn btn-primary" (click)="addOption()">Add Tion</button>
</clr-input-container>
</div>
</div>

<!-- <clr-input-container>
<label>Variation Option</label>
<input clrInput type="text" formControlName="optName"/>
</clr-input-container> -->
</div>
</div>
</div>

</div>
</div>

最佳答案

您的代码中存在输入错误,添加了额外的方括号

<div [formGroupName]="j"]> ,

更改为

<div [formGroupName]="j">

关于javascript - Angular 嵌套 FormArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705936/

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