Skip to content

Instantly share code, notes, and snippets.

@BPagoaga
Created June 4, 2018 11:37
Show Gist options
  • Save BPagoaga/7afe48f186b081da29d03cde825c6252 to your computer and use it in GitHub Desktop.
Save BPagoaga/7afe48f186b081da29d03cde825c6252 to your computer and use it in GitHub Desktop.
Group of checkboxes in an angular reactive form
<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
<form [formGroup]="testForm" (ngSubmit)="onSubmitForm()">
<div class="form-group" [formGroup]="testForm.controls.availableDaysForm">
<label for="firstName">Prénom</label>
<input type="checkbox" class="form-control" formControlName="lundi">
<input type="checkbox" class="form-control" formControlName="mardi">
</div>
<button type="submit" class="btn btn-primary">Soumettre</button>
</form>
</div>
import { Component, OnInit } from "@angular/core";
import { FormGroup, FormArray, FormBuilder, FormControl } from "@angular/forms";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
title = "app";
testForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit(): void {
//Called after the constructor, initializing input properties, and the first call to ngOnChanges.
//Add 'implements OnInit' to the class.
this.initForm();
}
initForm() {
this.testForm = this.fb.group({
availableDaysForm: this.fb.group({
lundi: false,
mardi: false
})
});
}
onSubmitForm() {
console.log(this.testForm.value);
}
}
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { ReactiveFormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, ReactiveFormsModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment