[英]How to set initial value for formArray?
I need to set initial value formArray f0r my form.我需要为我的表单设置初始值 formArray f0r。 Check my code:
检查我的代码:
Html: Html:
<div class="form-row form-group ingredients-drop">
<button class="button" (click)="addAlias()">Add ingredients</button>
<label>Kolicina</label>
<div *ngFor="let ingredient of ingredients.controls; let i=index">
<input class="select add-ingredients" type="text" [formControlName]="i">
</div>
</div>
TS: TS:
allRecepies: any[] = [];
selectedMeal: any
form: FormGroup;
ngOnInit() {
this.getAllRecepies();
}
onChange(event) {
this.allRecepies.map(obj => {
if (obj.id == event.target.value) {
this.selectedMeal = Object.assign(obj);
console.log(this.form.value);
}
})
this.editCity();
}
getAllRecepies() {
this.mealService.getRecepies().subscribe(
data => {
this.allRecepies = data;
}
)
}
editCity() {
this.form.patchValue({
name: this.selectedMeal.name ? this.selectedMeal.name : null,
description: this.selectedMeal.description ? this.selectedMeal.description : null,
preparationTime: this.selectedMeal.preparationTime ? this.selectedMeal.preparationTime : null,
pictureUrl: this.selectedMeal.pictureUrl ? this.selectedMeal.pictureUrl : null,
ingredients: this.selectedMeal.ingredients ? this.addMealArr() : null
})
}
addMealArr() {
if(this.selectedMeal) {
let a = this.selectedMeal.ingredients.map(obj => { return obj });
return a;
}
}
createForm() {
this.form = this.formBuilder.group({
name: [null],
description: [null],
preparationTime: [null],
pictureUrl: [''],
ingredients: this.formBuilder.array([
this.formBuilder.control(this.addMealArr())
])
});
}
get ingredients() {
return this.form.get('ingredients') as FormArray;
}
addAlias() {
this.ingredients.push(this.formBuilder.control(''));
}
This is maybe bad practice but I need tutorial for my problem or stackblitz or can anybody help me?这可能是不好的做法,但我需要针对我的问题或 stackblitz 的教程,或者有人可以帮助我吗? I need to set initial value with patchValue if it possible?
如果可能的话,我需要用 patchValue 设置初始值吗? Or to use another method?
还是使用其他方法? I don't know just send me tutorial or change my code or set stackblitz?
我不知道只是给我发教程或更改我的代码或设置 stackblitz? I need set initial values and add new row or delete existing..
我需要设置初始值并添加新行或删除现有的..
Hey you can set initial values in FormGroup as below:嘿,您可以在 FormGroup 中设置初始值,如下所示:
recipes = [
{
ingredients: [
{name: 'Grapes', amount: 12},
{name: 'Potato', amount: 12}
]
},
{
ingredients: [
{name: 'Banana', amount: 12},
{name: 'Orange', amount: 12}
]
}
];
let recipeIngredients = new FormArray([]);
const recipe = this.recipes[0];
if (recipe["ingredients"]) {
for (let ingredient of recipe.ingredients) {
recipeIngredients.push(
new FormGroup({
name: new FormControl(ingredient.name, Validators.required),
amount: new FormControl(ingredient.amount, [Validators.required])
})
);
}
}
this.recipeForm = new FormGroup({
ingredients: recipeIngredients
});
I have created working stackblitz example for the same demo我为同一个演示创建了工作 stackblitz 示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.