Я пытаюсь создать форму для рецептов с вложенным динамическим вводом ингредиентов.Я не знаю, что я делаю не так.это ошибка консоли браузера:
Cannot find control with path: 'ingredients -> 0 -> 0'
это мой HTML
<div formArrayName="ingredients">
<div *ngFor="let ingredient of recipeForm.get('ingredients')['controls']; let i=index" [formGroupName]="i">
<!-- address header, show remove button when more than one address available -->
<div>
<span>Zutat {{i + 1}}</span>
<!-- <span *ngIf="recipeForm.get('ingredients')['controls'].length > 1" (click)="removeIngredient(i)">
</span> -->
</div>
<!-- Angular assigns array index as group name by default 0, 1, 2, ... -->
<div [formGroupName]="i">
<!--ingredient-->
<div [formGroup]="ingredient">
<div class="form-group col-xs-6">
<input type="text" class="form-control" formControlName="newIngredient">
</div>
<div class="btn btn-success" (onclick)="addIngredient()">neue Zutat</div>
</div>
</div>
</div>
</div>
, и это мой TS-файл
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, Validator, Validators } from '@angular/forms';
import { NewRecipe } from '../recipe.interface';
import { validateConfig } from '@angular/router/src/config';
@Component({
selector: 'app-recipe-creator',
templateUrl: './recipe-creator.component.html',
styleUrls: ['./recipe-creator.component.css']
})
export class RecipeCreatorComponent implements OnInit {
constructor(private formBuilder: FormBuilder) {}
recipeForm: FormGroup;
ingredients: FormArray;
instructions: FormArray;
ngOnInit() {
this.recipeForm = this.formBuilder.group({
name: '',
category: '',
prep_time: 0,
cooking_time: 0,
price: 0,
ingredients: this.formBuilder.array([this.createIngredient()]),
instructions: this.formBuilder.array([this.createInstruction()])
});
}
createIngredient(): FormGroup {
return this.formBuilder.group({
newIngredient: ['']
});
}
createInstruction(): FormGroup {
return this.formBuilder.group({
newInstruction: ['']
});
}
addIngredient(): void {
this.ingredients = this.recipeForm.get('ingredient') as FormArray;
this.ingredients.push(this.createIngredient());
}
addInstruction(): void {
this.instructions = this.recipeForm.get('instruction') as FormArray;
this.instructions.push(this.createInstruction());
}
saveRecipe() {}
removeIngredient(i) {}
}
Ядовольно плохо знаком с угловой, и это сводит меня с ума.Обучающие программы по всем формам содержат код, который, похоже, не поддерживается Angular 6. Существует ли более новое учебное пособие о вложенных формах с FormArray где-нибудь?
Заранее спасибо.