angular - 动态添加响应式元素会使它们在 Angular 中自动失效
问题描述
我正在处理动态表单,其中的行是根据用户要求生成的。但问题是一旦用户生成新行,它就会自动变为无效,甚至没有触摸它。如何避免这种行为。这是我的表格
这是我的 html 文件
<form [formGroup]="loanProductForm">
<table>
<thead>
<tr class='tableHeader'>
<div fxLayout="row" fxLayoutAlign="start center" fxFlex="1 0 auto">
<td fxFlex="22" class="pr-4">Name</td>
<td fxFlex="15" class="pr-4">Price</td>
<td fxFlex="15" class="pr-4">Loan Term</td>
<td fxFlex="15" class="pr-4">Quantity</td>
<td fxFlex="15" class="pr-4">Deposit</td>
<td fxFlex="15" class="pr-4">Total</td>
</div>
</tr>
</thead>
<tbody>
<tr formArrayName="products" *ngFor="let product of loanProductForm.get('products').controls; let i = index">
<div [formGroupName]="i" fxLayout="row" fxLayoutAlign="start center" fxFlex="1 0 auto">
<td fxFlex="22">
<mat-form-field appearance="outline" fxFlex="100" class="pr-4">
<mat-label>Product </mat-label>
<mat-select formControlName="productId" [id]="'productId' + i" required>
<mat-option *ngFor="let product of productList" [value]="product.productId">
{{product.name}}
</mat-option>
</mat-select>
<div *ngIf="product.get('productId').errors?.required &&
product.get('productId').touched">
ProductId is required
</div>
</mat-form-field>
</td>
<td fxFlex="15">
<mat-form-field appearance="outline" fxFlex="100" class="pr-4">
<mat-label>Price </mat-label>
<input type='number' matInput formControlName="price" [id]="'price' + i" name="" placeholder="Price" required>
</mat-form-field>
</td>
<td fxFlex="15">
<mat-form-field appearance="outline" fxFlex="100" class="pr-4">
<mat-label>Loan Term </mat-label>
<mat-select formControlName="loanTermId" [id]="'loanTermId' + i" required>
<mat-option *ngFor="let loanTerm of loanTermList" [value]="loanTerm.loanTermId">
{{loanTerm.numberOfMonths}}
</mat-option>
</mat-select>
</mat-form-field>
</td>
<td fxFlex="15">
<mat-form-field appearance="outline" fxFlex="100" class="pr-4">
<mat-label>Quantity </mat-label>
<input type='number' formControlName="quantity" [id]="'quantity' + i" matInput name="" id="" placeholder="Quantity" required>
</mat-form-field>
</td>
<td fxFlex="15">
<mat-form-field appearance="outline" fxFlex="100" class="pr-4">
<mat-label>Deposit </mat-label>
<input type='number' formControlName="deposit" [id]="'deposit' + i" matInput name="" id="" placeholder="Deposit" required>
</mat-form-field>
</td>
<td fxFlex="15">
<mat-form-field appearance="outline" fxFlex="100" class="pr-4">
<mat-label>Total </mat-label>
<input type='number' formControlName="total" [id]="'total' + i" matInput name="" id="" placeholder="Total" required>
</mat-form-field>
</td>
</div>
</tr>
<tr>
<td fxFlex="10">
<div fxLayout="row" fxLayoutAlign="start center" fxFlex="1 0 auto">
<button mat-stroked-button class='addBtn btn-style-2' fxFlex='100' (click)='addProductButtonClick()'>Add
<mat-icon matSuffix>add_box</mat-icon>
</button>
</div>
</td>
</tr>
</tbody>
</table>
</form>
<button mat-raised-button fxFlex="15" class="btn-style-1" style="align-self: flex-end;margin-right: 30px;margin-top: 15px;" (click)="addNewLoan()">Save</button>
</form>
ts
这是我文件中的相关代码片段
this.loanProductForm = this._formBuilder.group({
products: this._formBuilder.array([
this.addProductFormGroup()
])
});
addProductFormGroup(): FormGroup {
return this._formBuilder.group({
productId: ['', Validators.required],
price: ['', Validators.required],
loanTermId: ['', Validators.required],
quantity: ['', Validators.required],
deposit: ['', {Validators.required,updateOn: 'blur'}],
total: ['', Validators.required],
});
}
addProductButtonClick(): void {
(<FormArray>this.loanProductForm.get('products')).push(this.addProductFormGroup());
console.log('Loan Products: ', this.loanProductForm.value)
}
即使我*ngIf
在 html 中使用,但它仍然使新元素无效。
提前致谢
解决方案
我们需要记住的是,如果我们不将 angular 表单中的按钮指定为 type button
,则 angular 将假定按钮为 type submit
。所以这实际上是您的代码中发生的事情,您的表单实际上已提交。并且当提交表单时,默认的角度材质表单会将所有内容都涂成红色到无效字段。
所以只需添加
type="button"
到您的Add
按钮:
<button type="button" .... (click)='addProductButtonClick()'>
Add
</button>
推荐阅读
- node.js - 如何为 ExpressJS 路由保持 Nginx 上游?
- mysql - current_timestamp 错误对应于您的 MariaDB 服务器版本
- windows - Windows、Git 和裸存储库的 SSH url/路径
- selenium - 在无头和普通 chrome 之间弹出不同的 Google 登录
- powershell - Powershell脚本查看计算机是否设置为进入睡眠状态或显示器已关闭?
- angular - onSubmit 在 Angular 中触发两次
- keras - 添加用于从神经网络生成热图输出的注意力机制
- python - 应用 lambda 函数后,熊猫数据帧上的值不正确
- android - Kotlin 中的深拷贝领域对象
- angular - 空 FormGroup 的 setValue