Angular Reactive Form:如何预设值? (包含对象的 FormArray)
Angular Reactive Form : How to preset values ? (FormArray containing objects)
我正在尝试实现经典的“更新对象”功能。
我有一个 table,其中列出了我所有的产品,每一行都有一个编辑按钮,用于打开一个模式,我想在其中显示产品 属性 值。
我们这里关注的属性是'personas',它的值是一个对象数组,每个对象都是从下拉列表中选择的。
我设法显示了正确数量的对象(在该示例中为 3 个),但只是没有设置值,并且在每种情况下都选择了默认选项。
我尝试在 属性 上使用 angular 'patchValue()' 函数但没有成功。
这是我的打字稿:
public personas = new FormArray([], [], []);
constructor(private fb: FormBuilder, public productService: ProductService) {
this.productForm = this.fb.group({
personas: this.personas,
});
}
public onEdit(product: Product): void {
if (product.personas.length > 0) {
this.personaService.personas$.pipe(takeUntil(this._destroyed$)).subscribe((personas: Persona[]) => {
const array = product.personas.filter(p => personas.some(p2 => p.id === p2.id));
this.personas = this.fb.array(array.map(persona => new FormControl({
id: persona.id,
persona: persona.persona,
})));
});
}
}
这就是我的模板中的内容:
<div class="form-group">
<label for="personas">Personas</label>
<div id="personas" formArrayName="personas">
<div *ngFor="let persona of personas.controls; let i = index;" class="d-flex m-1">
<select
*ngIf="!(personaService.isFetchingPersonas$ |async) && (personaService.personas$ |async) as options"
class="form-control">
<option *ngFor="let option of options" [ngValue]="option">{{ option.persona }}</option>
</select>
</div>
</div>
感谢您的帮助
!!!编辑 !!!
我意识到,当我试图实现我的目标时,我打破了创建新产品的过程,因为角色 属性 不再正确设置。
因此 Stackblitz 项目与上述代码片段略有不同。
因为我将我的项目状态移回正常运行时。
STACKBLITZ :https://angular-ivy-cnmqhx.stackblitz.io
在 hello.component.html
的底部,我添加了这个小帮手来查看表单中的内容。
<pre>
{{ productForm.value | json }}
</pre>
然后我将 ngOnInit
更改为如下所示:
public ngOnInit(): void {
this.product.personas.forEach((persona: Persona) => {
const option = this.options.find(x => x.id === persona.id);
if (option) {
this.addField(option);
}
});
}
这里的想法是查找每个“选项”并将该选项放入 formArray,而不是“persona”。
还有一点简化的addField
方法
public addField(persona = {}): void {
(this.productForm.get("personas") as FormArray).push(
new FormControl(persona)
);
}
我正在尝试实现经典的“更新对象”功能。
我有一个 table,其中列出了我所有的产品,每一行都有一个编辑按钮,用于打开一个模式,我想在其中显示产品 属性 值。
我们这里关注的属性是'personas',它的值是一个对象数组,每个对象都是从下拉列表中选择的。
我设法显示了正确数量的对象(在该示例中为 3 个),但只是没有设置值,并且在每种情况下都选择了默认选项。
我尝试在 属性 上使用 angular 'patchValue()' 函数但没有成功。
这是我的打字稿:
public personas = new FormArray([], [], []);
constructor(private fb: FormBuilder, public productService: ProductService) {
this.productForm = this.fb.group({
personas: this.personas,
});
}
public onEdit(product: Product): void {
if (product.personas.length > 0) {
this.personaService.personas$.pipe(takeUntil(this._destroyed$)).subscribe((personas: Persona[]) => {
const array = product.personas.filter(p => personas.some(p2 => p.id === p2.id));
this.personas = this.fb.array(array.map(persona => new FormControl({
id: persona.id,
persona: persona.persona,
})));
});
}
}
这就是我的模板中的内容:
<div class="form-group">
<label for="personas">Personas</label>
<div id="personas" formArrayName="personas">
<div *ngFor="let persona of personas.controls; let i = index;" class="d-flex m-1">
<select
*ngIf="!(personaService.isFetchingPersonas$ |async) && (personaService.personas$ |async) as options"
class="form-control">
<option *ngFor="let option of options" [ngValue]="option">{{ option.persona }}</option>
</select>
</div>
</div>
感谢您的帮助
!!!编辑 !!!
我意识到,当我试图实现我的目标时,我打破了创建新产品的过程,因为角色 属性 不再正确设置。
因此 Stackblitz 项目与上述代码片段略有不同。 因为我将我的项目状态移回正常运行时。
STACKBLITZ :https://angular-ivy-cnmqhx.stackblitz.io
在 hello.component.html
的底部,我添加了这个小帮手来查看表单中的内容。
<pre>
{{ productForm.value | json }}
</pre>
然后我将 ngOnInit
更改为如下所示:
public ngOnInit(): void {
this.product.personas.forEach((persona: Persona) => {
const option = this.options.find(x => x.id === persona.id);
if (option) {
this.addField(option);
}
});
}
这里的想法是查找每个“选项”并将该选项放入 formArray,而不是“persona”。
还有一点简化的addField
方法
public addField(persona = {}): void {
(this.productForm.get("personas") as FormArray).push(
new FormControl(persona)
);
}