如何通过 API 绑定 FormArray 检索?
How to bind FormArray retrieve by API?
以下是我如何从 API 检索数据并绑定到 angular 中的 FormGroup 元素:
this.api.GetRecipe(id).subscribe((data) => {
this.form = this.fb.group({
nome: [data.nome, [Validators.required]],
ingredienti: this.fb.group({
acqua: [data.ingredienti.acqua, [Validators.required]]
})
});
});
但我不知道如何绑定数组。比如在那里检索farine
(这是一个对象数组):
this.api.GetRecipe(id).subscribe((data) => {
this.form = this.fb.group({
nome: [data.nome, [Validators.required]],
ingredienti: this.fb.group({
acqua: [data.ingredienti.acqua, [Validators.required]],
farine: this.fb.array([
this.fb.group({
quantita: [data.ingredienti.farine[x].quantita, [Validators.required]],
forza: [data.ingredienti.farine[x].forza, [Validators.required]]
})
])
})
});
});
当然 [x]
不在任何地方。每次迭代我在哪里检索 x?或者将数据从 API 传播到每个 FormGroup 的方式是什么?拉姆达?
使用map方法创建formGroup数组
this.api.GetRecipe(id).subscribe((data) => {
this.form = this.fb.group({
nome: [data.nome, [Validators.required]],
ingredienti: this.fb.group({
acqua: [data.ingredienti.acqua, [Validators.required]],
farine: this.fb.array(
data.ingredienti.farine.map(ingredient=>(
this.fb.group({
quantita: [ingredient.quantita, [Validators.required]],
forza: [ingredient.forza, [Validators.required]]
})))
)
})
});
});
以下是我如何从 API 检索数据并绑定到 angular 中的 FormGroup 元素:
this.api.GetRecipe(id).subscribe((data) => {
this.form = this.fb.group({
nome: [data.nome, [Validators.required]],
ingredienti: this.fb.group({
acqua: [data.ingredienti.acqua, [Validators.required]]
})
});
});
但我不知道如何绑定数组。比如在那里检索farine
(这是一个对象数组):
this.api.GetRecipe(id).subscribe((data) => {
this.form = this.fb.group({
nome: [data.nome, [Validators.required]],
ingredienti: this.fb.group({
acqua: [data.ingredienti.acqua, [Validators.required]],
farine: this.fb.array([
this.fb.group({
quantita: [data.ingredienti.farine[x].quantita, [Validators.required]],
forza: [data.ingredienti.farine[x].forza, [Validators.required]]
})
])
})
});
});
当然 [x]
不在任何地方。每次迭代我在哪里检索 x?或者将数据从 API 传播到每个 FormGroup 的方式是什么?拉姆达?
使用map方法创建formGroup数组
this.api.GetRecipe(id).subscribe((data) => {
this.form = this.fb.group({
nome: [data.nome, [Validators.required]],
ingredienti: this.fb.group({
acqua: [data.ingredienti.acqua, [Validators.required]],
farine: this.fb.array(
data.ingredienti.farine.map(ingredient=>(
this.fb.group({
quantita: [ingredient.quantita, [Validators.required]],
forza: [ingredient.forza, [Validators.required]]
})))
)
})
});
});