英文:
I'm trying to generate form control dynamically in angular forms & i'm facing that error in console "TypeError: feature_r5.get is not a function"
问题
<div id="features" class="tab-pane mytab-pane">
<div class="form-group">
<button type="button" class="btn btn-primary" (click)="addFeatureButtonClick()">添加</button>
</div>
<div formArrayName="features" *ngFor="let feature of advForm.get('features')?.value; let i = index">
<div attr.formGroupName="{{i}}">
<div class="form-group" [ngClass]="{'has-error' : feature.get('fName').invalid &&
feature.get('fName').touched}">
<div class="input-group">
<span class="input-group-prepend">
<label class="input-group-text"><strong>特性</strong></label>
</span>
<input type="text" id="{{'fName'+i}}" name="fName" class="form-control" placeholder="特性"
formControlName="fName"/>
</div>
<span class="help-block" *ngIf="feature.get('fName').errors.required &&
feature.get('fName').touched">
特性是必填项
</span>
</div>
</div>
</div>
</div>
英文:
I'm trying to generate form controls dynamically on click of a button in Angular v14 html template i generate it but i'm facing that error in console.
ERROR TypeError: feature_r5.get is not a function
at PostAdvComponent_div_40_Template
Html template code
<div id="features" class="tab-pane mytab-pane">
<div class="form-group">
<button type="button" class="btn btn-primary" (click)="addFeatureButtonClick()">Add</button>
</div>
<div formArrayName="features" *ngFor="let feature of advForm.get('features')?.value; let i = index">
<div attr.formGroupName="{{i}}">
<div class="form-group" [ngClass]="{'has-error' : feature.get('fName').invalid &&
feature.get('fName').touched}">
<div class="input-group">
<span class="input-group-prepend">
<label class="input-group-text"><strong>Features</strong></label>
</span>
<input type="text" id="{{'fName'+i}}" name="fName" class="form-control" placeholder="Features"
formControlName="fName"/>
</div>
<span class="help-block" *ngIf="feature.get('fName').errors.required &&
feature.get('fName').touched">
Feature is required
</span>
</div>
</div>
</div>
</div>
答案1
得分: 0
Sure, here are the translated parts:
-
不要遍历值(否则当您更改输入时,Angular 会重新绘制,您会失去焦点):
*ngFor="let feature of advForm.get('features')?.value;.." //<--WRONG
您需要使用一个 "getter"
get featureFormArray() { return this.form.get('features') as FormArray }
并遍历 "controls"
*ngFor="let feature of featureFormArray.controls;.." //<--OK
-
formGroupName 不是 属性,而是指令,所以
<div attr.formGroupName="{{i}}"> //<--WRONG <div [formGroupName]="i"> //<---OK
-
当管理 formArray 的 formGroup 时,我更喜欢使用这种结构
(formArrayName 在循环外部,循环中相同的标记,formGroupName):<form [formGroup]="form"> <div formArrayName="features"> <div *ngFor="let feature of featuresFormArray.controls;let i=index" [formGroupName]="i"> ...您的输入.. <input formControlName="fName"> </div> </div> </form>
-
在
.get
后面您需要使用安全操作符(请参阅点之前的 ?),否则您会出现 "可能为 null 或未定义" 的错误feature.get('fName')?.invalid && feature.get('fName')?.touched
-
在检查 errors.required 时同样如此
*ngIf="feature.get('fName')?.errors?.required
英文:
-
Not iterate over the values (else when you change an input, Angular redraw and you loose the focus):
*ngFor="let feature of advForm.get('features')?.value;.." //<--WRONG
You need use a "getter"
get featureFormArray() { return this.form.get('features') as FormArray }
and iterate over "controls"
*ngFor="let feature of featureFormArray.controls;.." //<--OK
-
formGroupName is not an attribute, else a directive, so
<div attr.formGroupName="{{i}}"> //<--WRONG <div [formGroupName]="i"> //<---OK
-
When manage fromGroup of formArray I like more use this structure
(the formArrayName outside the loop, the same tag in loop, the
formGroupName):<form [formGroup]="form"> <div formArrayName="features"> <div *ngFor="let feature of featuresFormArray.controls;let i=index" [formGroupName]="i"> ...yours inputs.. <input formControlName="fName"> </div> </div> </form>
-
You need use the safe operator (see the ? before the dot) after the
.get else you have an error of "possible null or undefined"feature.get('fName')?.invalid && feature.get('fName')?.touched
-
Same before when you check errors.required
*ngIf="feature.get('fName')?.errors?.required
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论