Home >Web Front-end >JS Tutorial >A brief analysis of how to use FormArray and modal boxes in Angular
How to use FormArray and modal box together? The following article will introduce to you how to use Angular's FormArray and modal box together. I hope it will be helpful to you!
Use FormArray to create dynamic forms. Every time a form is created, a new input is added to the page to display the title of the form. Click Edit and jump to click the fill-in content of the form. [Related tutorial recommendations: "angular tutorial"]
// 封装获取modelList get modelList() { return this.formGroup.get('modelList') as FormArray } constructor(private fb: FormBuilder) {} ngOnInit() { // 一开始初始化arr为空数组 this.formGroup = this.fb.group({ // 内部嵌套FormControl、FormArray、FormGroup modelList: this.fb.array([]) }) } // 模态框构造内部的表单 function newModel() { return this.fb.group({ modelName: [''], // 可以继续嵌套下去,根据业务需求 }) } // 省略模态框部分代码 // 传递到模态框的FormArray selectedType: FormArray
<form [FormGroup]="formGroup"> <div FormArrayName="modelList"> <ng-container *nfFor="let item of modelList.controls;let i = index" [FormGroupName]="i"> <nz-input-group [nzSuffix]="suffixIconSearch" > <input type="text" nz-input formControlName="modelName"/> </nz-input-group> <ng-template #suffixIconSearch> <span nz-icon nzType="edit" class="hover" (click)="showModal(i)" ></span> </ng-template> </ng-container> </div> </form> <nz-modal [(nzVisible)]="isVisible" nzTitle="Model" [nzFooter]="modalFooter" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()" > <ng-container *nzModalContent> <form nz-form [formGroup]="selectedType"> <nz-form-item> <nz-form-label nzRequired>Model Test</nz-form-label> <nz-form-control> <input type="text" nz-input placeholder="请输入ModelName" formControlName="modelName" /> </nz-form-control> </nz-form-item> <nz-form-item> <nz-form-control> <product-config></product-config> </nz-form-control> </nz-form-item> </form> </ng-container> <ng-template #modalFooter> <button *ngIf="!isNewModel" nzDanger nz-button nzType="default" (click)="handleDelete()">删除</button> <button *ngIf="isNewModel" nz-button nzType="default" (click)="handleCancel()">取消</button> <button nz-button nzType="primary" (click)="handleOk()">保存</button> </ng-template> </nz-modal>
Because this kind of modal box is special, it separates the relationship between the FormGroup of the form and needs to be passed when clicking. The parameters are sent to the modal box to display part of the value. If you simply pass the parameters and use this.modelList.at(index)
to obtain the entity and assign the value to the modal box for modification, you will find the modification after clicking Save in the modal box. The value is not updated in the form, and modifications to the input value on the form are found to affect the content of the modal box.
But the form added by the modal box can be responded to the page.
After clicking edit, pass the clicked FormArray element to a temporary variable this.selectedType = <formgroup>this.modelList.at(index);</formgroup>
, and pass the value to the modal box form.
Click to save the modal box and replace the original FormArray value again
this.modelList.removeAt(this.modelIndex) this.modelList.insert(this.modelIndex, this.selectedType)
newModelType(): FormGroup { return this.fb.group({ modelName: ['', Validators.required], configList: this.fb.array([]), }); } // ...省略 // 模态框显示 show() { this.isVisible = true this.selectedType = this.newModelType(); } // 保存 save() { this.isVisible = false // 原页面FormArray this.modelList.push(this.selectedType); }Finally found that this writing method can only be used in one direction Change, the modified value of the input outside the page will affect the modal box, but the value of the modal box is changed and saved, but the outside is not updated. Checking the internal parameters of the FormArray of the page through the console method found that there were actually changes, but Angular did not detect it. At this time, it is judged that the reason why there is no response is generally that the angular detection mechanism is not triggered. After carefully checking the document, I found that there is a very important line
angular document at the bottom that says
this.selectedType = this.newModelType(); const old = this.modelList.at(index); this.selectedType.setValue({ 'modelName': old.get('modelName').value })You can update normally at this time.
Programming Teaching! !
The above is the detailed content of A brief analysis of how to use FormArray and modal boxes in Angular. For more information, please follow other related articles on the PHP Chinese website!