I have an dynamic array of department list fetched from server. I want to push that array to form array on initialization, basically i want to show checkboxes based on department name or id in array. I know how to push an empty array in reactive forms.but how to initialize with an existing array.Actually its an update/edit Component
departmentList:any=[]; //array contains all departments
SelectedDeptList:any=[]; //fetched from db , selected departments
userForm: FormGroup;
this.userForm = this.fb.group({ //fb form builder
'phone': [null],
'departmentList': this.fb.array([this.createDepartment()]),
})
createDepartment(): FormGroup {
return this.fb.group({
'name': ''//checkbox value true or false
});
}
Template
<div formArrayName="departmentList" *ngFor="let item of
userForm.get('departmentList').controls; let i = index;">
<div class="col-md-6" [formGroupName]="i">
<div class="form-group">
<div class="col-md-4">
<label class="mt-checkbox mt-checkbox-outline">
<input formControlName="name" type="checkbox"> Department Name
<span></span>
</label>
</div>
</div>
</div>
</div>
ToDo ?
- how can i populate or initialize list of all dept checkboxes and those should be true which are present or exist in my 'SelectedDeptList' array(fetched from db).
thanks in advance , any suggestion will be appreciated.