angular - Angular - 在编辑时填充表单数组输入
问题描述
我有一个数量字段和一个“添加”按钮。单击按钮时,会添加数量的新字段以及删除按钮以将其删除。我已经为它使用了表单数组
我的代码如下
<div formArrayName="ingredients11">
<!-- loop throught units -->
<div *ngFor="let unit of recipeForm['controls'].ingredients11['controls']; let i = index ">
<div [formGroupName]="i">
<div class="form-group">
<label>Quantity</label>
<input type="text" class="form-control" formControlName="recipe_ingredient_quantity">
<div *ngIf="unit['controls'].recipe_ingredient_quantity.invalid" class="alert alert-danger">
<div *ngIf="unit['controls'].recipe_ingredient.errors.required">
Quantity is required.
</div>
</div>
</div>
<button class="btn btn-danger" *ngIf="recipeForm['controls'].ingredients11['controls'].length > 1" (click)="remove(i)">Delete</button>
</div>
</div>
<button class="btn btn-primary" (click)="add()">Add</button>
</div>
我还有一个编辑功能,我想预先填充数量字段的数量及其先前保存的值以进行编辑
我的代码如下:
<div formArrayName="ingredients11">
<!-- loop throught units -->
<div *ngFor="let unit of editrecipeForm['controls'].ingredients11['controls']; let i = index ">
<!-- row divider show for every nex row exclude if first row -->
<div *ngIf="editrecipeForm['controls'].ingredients11['controls'].length > 1 && i > 0" ><hr></div>
<div [formGroupName]="i" *ngFor="let ri of editingredientsarray;">
<div class="form-group">
<label>Ingredients</label>
<select class="form-control" formControlName="recipe_ingredient" >
<option value="">Select Ingredient</option>
<option *ngFor="let ingredient of ingredients | async" [value]="ingredient.id">
{{ingredient.name}}
</option>
</select>
<div *ngIf="unit['controls'].recipe_ingredient.invalid" class="alert alert-danger">
<div *ngIf="unit['controls'].recipe_ingredient.errors.required">
Ingredient is required.
</div>
</div>
</div>
<div class="form-group">
<label>Quantity</label>
<input type="text" class="form-control" formControlName="recipe_ingredient_quantity" [value]="ri.quantity">
<div *ngIf="unit['controls'].recipe_ingredient_quantity.invalid" class="alert alert-danger">
<div *ngIf="unit['controls'].recipe_ingredient.errors.required">
Quantity is required.
</div>
</div>
</div>
<button class="btn btn-danger" *ngIf="editrecipeForm['controls'].ingredients11['controls'].length > 1" (click)="removeEditIngredient(i)(i)">Delete Ingredient</button>
</div>
</div>
<button class="btn btn-primary" (click)="addEditIngredient()">Add New Ingredient</button>
</div>
但是该值未填充在输入字段中。任何人都可以请指导
解决方案
您的状态配置错误:
export const Users = [
{
"id": "1",
"name": "aaa",
"technology": "1" //<------ Use id instead of names
},
{
"id": "2",
"name": "bbb",
"technology": "1,2" //<------ Use id instead of names
},
{
"id": "3",
"name": "ccc",
"technology": "1,3" //<------ Use id instead of names
}
]
推荐阅读
- javascript - 为什么 NodeJS/Socket 调用这么多次
- visual-studio - 如何修复 Visual Studio 错误“无法加载项目文件。根级别的数据无效。”
- python - threading.Lock() 是否与 QThread() 兼容,QMutex() 是否与 python 线程兼容?
- google-api - 无法编辑自动创建的“G Suite Marketplace 集成客户端”OAuth 2.0 客户端 ID
- java - 如何手动处理 Spring Data JPA 分页和排序?
- r - 根据不同的条件重新编码多个列
- security - 在 Google OAuth 工作流程中何时使用私钥?
- android - Admob - 部署几天后未显示横幅广告
- javascript - 如何使用 javascript 从列 id 获取值到另一个页面?
- r - r:通过函数运行所有数据帧以获取最小和最大日期