Skip to content

Commit 7ae3ce4

Browse files
mmalerbawagnermaciel
authored andcommitted
docs(material/checkbox): add example of using mat-checkbox with reactive forms (#22796)
(cherry picked from commit 47b7357)
1 parent d1743b9 commit 7ae3ce4

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.example-section {
2+
margin: 12px 0;
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<section class="example-section" [formGroup]="toppings">
2+
<h4>Select your toppings:</h4>
3+
<p><mat-checkbox formControlName="pepperoni">Pepperoni</mat-checkbox></p>
4+
<p><mat-checkbox formControlName="extracheese">Extra Cheese</mat-checkbox></p>
5+
<p><mat-checkbox formControlName="mushroom">Mushroom</mat-checkbox></p>
6+
</section>
7+
8+
<section class="example-section" [formGroup]="toppings">
9+
<h4>You chose:</h4>
10+
{{toppings.value | json}}
11+
</section>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Component} from '@angular/core';
2+
import {FormBuilder, FormGroup} from '@angular/forms';
3+
4+
/** @title Checkboxes with reactive forms */
5+
@Component({
6+
selector: 'checkbox-reactive-forms-example',
7+
templateUrl: 'checkbox-reactive-forms-example.html',
8+
styleUrls: ['checkbox-reactive-forms-example.css']
9+
})
10+
export class CheckboxReactiveFormsExample {
11+
toppings: FormGroup;
12+
13+
constructor(fb: FormBuilder) {
14+
this.toppings = fb.group({
15+
pepperoni: false,
16+
extracheese: false,
17+
mushroom: false
18+
});
19+
}
20+
}

src/components-examples/material/checkbox/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import {CommonModule} from '@angular/common';
22
import {NgModule} from '@angular/core';
3-
import {FormsModule} from '@angular/forms';
3+
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
44
import {MatCardModule} from '@angular/material/card';
55
import {MatCheckboxModule} from '@angular/material/checkbox';
66
import {MatRadioModule} from '@angular/material/radio';
77
import {CheckboxConfigurableExample} from './checkbox-configurable/checkbox-configurable-example';
8-
import {CheckboxOverviewExample} from './checkbox-overview/checkbox-overview-example';
98
import {CheckboxHarnessExample} from './checkbox-harness/checkbox-harness-example';
9+
import {CheckboxOverviewExample} from './checkbox-overview/checkbox-overview-example';
10+
import {
11+
CheckboxReactiveFormsExample
12+
} from './checkbox-reactive-forms/checkbox-reactive-forms-example';
1013

1114
export {
1215
CheckboxConfigurableExample,
1316
CheckboxOverviewExample,
1417
CheckboxHarnessExample,
18+
CheckboxReactiveFormsExample,
1519
};
1620

1721
const EXAMPLES = [
1822
CheckboxConfigurableExample,
1923
CheckboxOverviewExample,
2024
CheckboxHarnessExample,
25+
CheckboxReactiveFormsExample,
2126
];
2227

2328
@NgModule({
@@ -27,6 +32,7 @@ const EXAMPLES = [
2732
MatCheckboxModule,
2833
MatRadioModule,
2934
FormsModule,
35+
ReactiveFormsModule,
3036
],
3137
declarations: EXAMPLES,
3238
exports: EXAMPLES,

0 commit comments

Comments
 (0)