Я реализовал модал, который содержит флажки и кнопку отправки, как показано ниже. Я хотел бы проверить флажки в том, что по крайней мере один должен выбрать один элемент из флажка и отправить только выбранные элементы, не все из них. Когда я отправляю элементы, я отправляю все, что я не хочу делать.
Снимок экрана
Файл TS
...
this.form = this.fb.group({
checkArray: this.fb.array([], [Validators.required])
});
}
ngOnInit() {
this.getAllRemarks();
this.myform = this.fb.group({
otherInput: null,
// API key to bind list of items. e.g. [{id:1},{id:2}]
reason: null
});
}
onClose() {
this.dialogbox.close();
this.dataService.filter('Register click');
}
onCheckBoxChanges(e: HTMLInputElement, id: number) {
// get current position of the changes element by ID
const index = this.remarksList.findIndex(_ => _.id === id);
if (!(index > -1)) return;
// const isChecked = this.checkBoxes[index].isChecked;
// this.masterCheckBoxes[index].isChecked = e.checked;
}
onSubmit() {
// assign the changes value for the POST
this.myform.value['reason'] = this.remarksList;
console.log(this.myform.value);
}
...
HTML ФАЙЛ
<h2>Add Comments</h2>
<p>Reasons for declining the inspection</p>
<form [formGroup]="myform" (ngSubmit)="onSubmit()">
<div *ngFor="let check of this.remarksList;">
<label>
<input #el (change)="onCheckBoxChanges(el, check.id)" type="checkbox" [checked]="check.isChecked" />
{{check.comment}}
</label>
</div>
<br> <br>
Other: <input type="text" formControlName="otherInput" />
<br> <br>
<button type="submit">Submit</button>
</form>