Skip to content

Commit 2b6b2b5

Browse files
author
Your Name
committed
Angular Forms In Depth
1 parent 18e901d commit 2b6b2b5

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/app/create-course/create-course-step-3/create-course-step-3.component.html

+25-14
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,40 @@ <h3>Add Course Lessons:</h3>
33

44
<div class="add-lessons-form" [formGroup]="form">
55

6-
<div class="lesson-form-row">
6+
<ng-container formArrayName="lessons">
77

8-
<mat-form-field appearance="fill">
8+
<ng-container *ngFor="let lessonForm of lessons.controls">
99

10-
<input matInput placeholder="Lesson title">
10+
<div class="lesson-form-row" [formGroup]="lessonForm">
1111

12-
</mat-form-field>
12+
<mat-form-field appearance="fill">
1313

14-
<mat-form-field appearance="fill">
14+
<input matInput
15+
formControlName="title"
16+
placeholder="Lesson title">
1517

16-
<mat-select placeholder="Lesson level">
17-
<mat-option value="beginner">Beginner</mat-option>
18-
<mat-option value="intermediate">Intermediate</mat-option>
19-
<mat-option value="advanced">Advanced</mat-option>
20-
</mat-select>
18+
</mat-form-field>
2119

22-
</mat-form-field>
20+
<mat-form-field appearance="fill">
2321

24-
<mat-icon class="delete-btn">delete_forever</mat-icon>
22+
<mat-select formControlName="level"
23+
placeholder="Lesson level">
24+
<mat-option value="beginner">Beginner</mat-option>
25+
<mat-option value="intermediate">Intermediate</mat-option>
26+
<mat-option value="advanced">Advanced</mat-option>
27+
</mat-select>
2528

26-
</div>
29+
</mat-form-field>
2730

28-
<button mat-mini-fab>
31+
<mat-icon class="delete-btn">delete_forever</mat-icon>
32+
33+
</div>
34+
35+
</ng-container>
36+
37+
</ng-container>
38+
39+
<button mat-mini-fab (click)="addLesson()">
2940
<mat-icon class="add-course-btn">add</mat-icon>
3041
</button>
3142

src/app/create-course/create-course-step-3/create-course-step-3.component.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
1010
export class CreateCourseStep3Component {
1111

1212
form = this.fb.group({
13-
13+
lessons: this.fb.array([])
1414
});
1515

1616

1717
constructor(private fb:FormBuilder) {
1818

1919
}
2020

21+
get lessons() {
22+
return this.form.controls["lessons"] as FormArray;
23+
}
24+
25+
addLesson() {
26+
27+
const lessonForm = this.fb.group({
28+
title: ['', Validators.required],
29+
level: ['beginner', Validators.required]
30+
});
31+
32+
this.lessons.push(lessonForm);
33+
}
2134
}

0 commit comments

Comments
 (0)