Created
June 4, 2018 11:37
-
-
Save BPagoaga/7afe48f186b081da29d03cde825c6252 to your computer and use it in GitHub Desktop.
Group of checkboxes in an angular reactive form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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