.html页面
<pre>
<div *ngFor="let data of data;let i=index" class="row_wrap">
<div class="row_inner_wrap">
<div class="row">
<div class="col-md-12">
<div class="col-md-5 inner_row">
<mat-form-field appearance="outline" class="input-container">
<input (click)="readonly($event)" formControlName="status" matInput [value]="data?.employeeStatus" class="form-control" placeholder="{{ 'Status'| translate }}" readonly>
</mat-form-field>
<i class="fa fa-check edit_dept" data-uuid="" data-type="department"></i>
</div>
<div class="col-md-6 inner_row b4_del_header">
<mat-form-field appearance="outline" class="input-container">
<input matInput placeholder="{{ 'description'| translate }}" formControlName="description" (click)="readonly($event)" [value]=" data?.description" class="form-control" readonly>
</mat-form-field>
<div class="add_des_edit">
<div class="update_icon">
<i class="fa fa-check edit_dept" data-uuid="" data-type="department"></i>
</div>
</div>
</div>
<div *ngIf="!enableEdit" class="del_icon" (click)="ondelete(data?._id)">
<i class="fa fa-trash-o del del_department" aria-hidden="true" style="visibility: visible;" data-uuid=""></i>
</div>
<div class="add_icon" style="display: none"><i class="fa fa-plus add_row" data-uuid=""></i>
</div>
<div *ngIf="enableEdit" class="edit_icon" (click)="onUpdate()" style="display: inline-block;">
<i class="fa fa-check edit_row edit_row_department" data-uuid=""></i>
</div>
</div>
</div>
</div>
</div>.component.ts
{
constructor(private fb: FormBuilder,
private elRef: ElementRef,
private settingservice: SettingsService) {
this.emp_status = this.fb.group({
status: [],
description : []
});
}
}如何在模板中填充数组元素?有了这些代码,我得到了空字段。在检查时,我在输入字段中得到值,即使它显示空字段
发布于 2020-07-02 07:22:11
FormArrays简介
如果我们想要一个像
[
{status:'status 1',description:'description 1'},
{status:'status 2',description:'description 2'},
{status:'status 3',description:'description 3'},
...
]我们有一个对象数组,所以我们需要一个FormArray of FormGroups (*)。要创建它,它有一个返回FormGroup (FormArray的一个元素)的函数
createGroup():FormGroup
{
return new FormGroup({
status:new FormControl(),
description:new FormControl(),
})
}如果我们的FormArray属于FormGroup,它有一个返回FormArray的getter函数是有用的
form:FormGroup; //<--declare the form
get statusArray():FormArray
{
return this.form.get('emp_status') as FormArray
}
//in ngOnInit
ngOnInit()
{
this.form=new FormGroup({
other_property:new FormControl(),
emp_status=new FormArray([]) //<--see that we create an empty FormArray
})
}如果只希望FormArray本身只声明
formArray:FormArray
//in ngOnInit we create the formArray empty -well you can do it in the declaration-
ngOnInit()
{
this.formArray=new FormArray([]) //<--see that we create an empty FormArray
}现在,我们将向数组中添加元素。请记住,FormArray的每个元素都是一个FormGroup,但是我们有一个返回formGroup的函数!
因此,我们可以简单地添加
this.statusArray.push(this.createGroup()) //if we are using a formArray inside a FormGroup
this.formArray.push(this.createGroup()) //if we has the formarray standalone如果我们想将基于数组的formArray添加到数组中,我们通常会将数组“映射”到FormGroup并创建数组puff。假设您有一个数据数组,您可以这样做
this.form=new FormGroup({
other_property:new FormControl(),
emp_status=new FormArray(
this.data.map(x=>this.createGroup()) //<--each element of this data
//like a formGroup
)
})
//or with our standalone FormArray
this.formArray=new FormArray(this.data.map(x=>this.createGroup())) 好吧,在.html上看
先检查一下是否可以
<pre>
{{form?.value|json}}
</pre>
//or
<pre>
{{formArray.value|json}}
</pre>我们正在使用输入和div。
如果是在FormGroup中
<form [formGroup]="form">
<!--say to Angular we are going to use the formArray using formArrayName-->
<div formArrayName="emp_status">
<!--iterate over the controls of formArray and use [formGroupName]-->
<!--see that we use our getter function-->
<div *ngFor="let group of statusArray.controls;let i=index" [formGroupName]="i">
<!--use formControlName, we are yet in a formGroup-->
<input formControlName="status">
<input formControlName="description">
</div>
</div>
</form>如果FormArray是独立的,则严格模式存在问题。在我们使用之前
/**BEFORE***/
<div *ngFor="let group of formArray.controls;let i=index" [formGroup]="group">现在,我们创建一个辅助函数
getGroupAt(index)
{
return this.formArray.at(index) as FormGroup
}
<form [formGroup]="formArray">
<!--iterate over the controls of formArray and use [formGroup]-->
<div *ngFor="let group of formArray.controls;let i=index" [formGroup]="getGroupAt(i)">
<!--use formControlName, we are yet in a formGroup-->
<input formControlName="status">
<input formControlName="description">
</div>
</div>
</form>你可以在stackblitz中看到
更新了如何填充给定的数组数据
还记得我们写this.formArray=new FormArray(this.data.map(x=>this.createGroup()))的时候吗?
那写呢
this.formArray=new FormArray(this.data.map(x=>this.createGroup(x))) 那么,将函数createGroup更改为
createGroup(data:any=null):FormGroup
{
data=data || {status:null,description:null}
return new FormGroup({
status:new FormControl(data.status),
description:new FormControl(data.description),
})
}结束
(*)如果我们只想管理数字数组或字符串数组,那么FormArray可以是FormControls的FormArray
https://stackoverflow.com/questions/62689788
复制相似问题