|
1 |
| -# NgReactivefrm |
| 1 | +# Angular Reactive Forms with lots of Nesting |
2 | 2 |
|
3 |
| -This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.2.3. |
| 3 | +I looked everywhere for a solution I could just copy and paste into my project but with no luck, so here's my attempt at providing it for the next poor soul stuck in my situation. I've tried to keep the code as simple as possible. |
4 | 4 |
|
5 |
| -## Development server |
| 5 | +# You have a league |
6 | 6 |
|
7 |
| -Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. |
| 7 | +``` |
| 8 | +league_details: this.fb.group({ |
| 9 | + name: "", |
| 10 | + founder: "" |
| 11 | + }) |
| 12 | +``` |
8 | 13 |
|
9 |
| -## Code scaffolding |
| 14 | +# A league has teams |
10 | 15 |
|
11 |
| -Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. |
| 16 | +``` |
| 17 | +this.leagueForm = this.fb.group({ |
| 18 | + league_details: this.fb.group({ |
| 19 | + name: "", |
| 20 | + founder: "" |
| 21 | + }), |
| 22 | + teams: this.fb.array([this.teams]) |
| 23 | + }); |
| 24 | +``` |
12 | 25 |
|
13 |
| -## Build |
| 26 | +# Teams have names and players |
14 | 27 |
|
15 |
| -Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. |
| 28 | +``` |
| 29 | +get teams(): FormGroup { |
| 30 | + return this.fb.group({ |
| 31 | + team_name: "", |
| 32 | + players: this.fb.array([this.players]) |
| 33 | + }); |
| 34 | + } |
| 35 | +``` |
16 | 36 |
|
17 |
| -## Running unit tests |
| 37 | +# Players have names and numbers |
18 | 38 |
|
19 |
| -Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). |
| 39 | +``` |
| 40 | +get players(): FormGroup { |
| 41 | + return this.fb.group({ |
| 42 | + player_name: "", |
| 43 | + player_number: "" |
| 44 | + }); |
| 45 | + } |
| 46 | +``` |
20 | 47 |
|
21 |
| -## Running end-to-end tests |
| 48 | +# You have a FormGroup |
22 | 49 |
|
23 |
| -Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). |
| 50 | +``` |
| 51 | +<form [formGroup]="leagueForm"> |
| 52 | +``` |
24 | 53 |
|
25 |
| -## Further help |
| 54 | +# Show League Detail |
26 | 55 |
|
27 |
| -To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). |
| 56 | +``` |
| 57 | +<div class="form-header" formGroupName="league_details"> |
| 58 | + <label>League Name <input formControlName="name"/></label> |
| 59 | + <label>Founder <input formControlName="founder"/></label> |
| 60 | + </div> |
| 61 | +``` |
| 62 | + |
| 63 | +# Show Teams |
| 64 | + |
| 65 | +It's VITAL you have a wrapping div that defines formArrayName! |
| 66 | + |
| 67 | +``` |
| 68 | +<div formArrayName="teams"> |
| 69 | + <div class="teams" *ngFor="let team of leagueForm.get('teams').controls; |
| 70 | + let teamIndex = index" [formGroupName]="teamIndex"> |
| 71 | + <label>Team Name <input formControlName="team_name"/></label> |
| 72 | + <!-- Next section goes here --> |
| 73 | + </div> |
| 74 | +</div> |
| 75 | +``` |
| 76 | + |
| 77 | +# Show Players inside Teams |
| 78 | + |
| 79 | +Again, important you have a wrapping div with formArrayName. Also, you need to add players in context of the team. Notice in ngFor i am referencing the team variable |
| 80 | +defined in the ngFor loop above for teams. I will also need to reference this team when removing or adding players |
| 81 | + |
| 82 | +``` |
| 83 | +<div formArrayName="players"> |
| 84 | + <div class="players" *ngFor="let player of team.get('players').controls; |
| 85 | + let playerIndex=index" [formGroupName]="playerIndex"> |
| 86 | + <label>Player Name <input formControlName="player_name" /> |
| 87 | + <label>Player Number <input formControlName="player_number"/></label> |
| 88 | + </div> |
| 89 | +</div> |
| 90 | +``` |
| 91 | + |
| 92 | +# Adding Teams |
| 93 | + |
| 94 | +Place ADD button just above the div that defines the formArrayArrayName teams |
| 95 | + |
| 96 | +``` |
| 97 | + <button type="button" (click)="addTeam()">Add Team</button> |
| 98 | +``` |
| 99 | + |
| 100 | +``` |
| 101 | +addTeam() { |
| 102 | + (this.leagueForm.get("teams") as FormArray).push(this.teams); |
| 103 | + } |
| 104 | +``` |
| 105 | + |
| 106 | +#Adding Players |
| 107 | +Place ADD button just above the div that defines the formArrayName players, and remember to pass in the team variable from the enclosing ngFor loop. |
| 108 | + |
| 109 | +``` |
| 110 | +<button type="button" (click)="addPlayer(team)">Add Player</button> |
| 111 | +``` |
| 112 | + |
| 113 | +``` |
| 114 | +addPlayer(team) { |
| 115 | + team.get("players").push(this.players); |
| 116 | + } |
| 117 | +``` |
0 commit comments