所以我试图传递我在对话框中输入的数据并将其发送回父组件,我的问题是如何在关闭后修补数组的值?我尝试了patchvalue和setvalue,它不会显示?任何人都可以帮助我
垫对话框
OnInit(): void {
this.fg = this.fb.group({
id: [],
qty: [],
displayArray: this.fb.array([this.CreateItem()]),
});
this.fg.get('qty').valueChanges.subscribe((res) => {
for (let i = this.displayArray.length; i < res; i++) {
this.displayArray.push(this.CreateItem());
}
});
this.data = this.fg.get('qty').setValue(this.data.qty);
}
CreateItem(): FormGroup {
return this.fb.group({
MoreItem: [''],
});
}
get displayArray(): FormArray {
return this.fg.get('displayArray') as FormArray;
}
inMemoryList() {
this.http.get<MyDTO>('api/users').subscribe((test) => (this.myDTO = test));
}
save() {
this.dialogRef.close([this.data.qty]);
}
我的父组件
CreatesetItem(): FormGroup {
return this.fb.group({
ListItem: [''],
});
}
item(): FormGroup {
return this.fb.group({
qty: [],
name: [''],
});
}
get setItem(): FormArray {
return this.fg.get('setItem') as FormArray;
}
get Info(): FormArray {
return this.fg.get('Info') as FormArray;
}
openAlertDialog(index: number) {
const dialogRef = this.dialog.open(AlertDialogComponent, {
data: {
qty: this.fg.value.Info[index].qty,
name: this.fg.value.Info[index].name,
},
});
dialogRef.afterClosed().subscribe((result) => {
for (let i = 0; i < result; i++) {
this.setItem.push(this.CreatesetItem());
}
for (let i = 0; i < result; i++) {
this.setItem.at(i).patchValue({ qty: result.qty });
}
});
}
添加(更改)甚至下拉选择,然后使用[(ngModel)]="variable"填充这些空框的值
app.html
<select (change)="setSelectedItem($event)">
<option>option1</ng-option>
<option>option2</ng-option>
</select>
<input type="text" [(ngModel)]="field1" name="field1">
<input type="text" [(ngModel)]="field2" name="field2">
app.component.ts
// declare variables
field1: string;
field2: string;
// dropdown on change function
setSelectedItem(event) {
// set values to those variables
this.field1 = "Hi";
this.field2 = "Hello";
}