我有一个表单,我使用表单数组。在getstudentsArray中,我获取学生的数据。我想用ngfor在formarray中显示这个动态数据。请帮我绑定getstudentsArray数组在html形式数组你能帮我解决这个问题吗?提前谢谢。
HTML
<span formArrayName="times">
<a (click)="addGroup()">Add New Textfield</a>
<span *ngFor="let time of timesArray.controls; let i = index;">
<span [formGroupName]="i">
<input type="text" class="form-control" formControlName="lists" placeholder="enter dropdown options">
</span>
</span>
</span>
getstudentsArray: any = [];
getStudentDetails(){
const obj: any = {
ID: this.stu_id
}
this.service.postmethod('studentsDetails', obj).subscribe((res: any) => {
this.respon= res;
this.getstudentsArray = this.respon.test;
});
}
{
"test": [
{"id": 1,"class": 5,"name": "john"},
{"id": 2,"class": 5,"name": "tim"},
{"id": 3,"class": 5,"name": "alex"},
]
}
这是你的工作解决方案:https://stackblitz.com/edit/angular-xeuupi
首先你需要创建对象的formgroup,然后你需要把这些组推到数组。我还添加了验证器功能,如果你想添加。
对于验证器,你可以说:
this.validators = {
'name': Validators.compose([Validators.required, Validators.maxLength(50)])
};
你需要传递this.validator在createFormGroup(…)方法中替换空白对象{}