|
| 1 | + |
| 2 | +<!-- |
| 3 | + You don't strictly need a Form when dealing with form inputs. However, since I am |
| 4 | + also using an NgModelGroup for my "user" inputs, I need to provide a "container" |
| 5 | + within which that group will register itself. Plus, the Form gives you access to |
| 6 | + the submit event and the NgForm export reference. |
| 7 | +--> |
| 8 | +<form #ngForm="ngForm" (submit)="processForm( ngForm )"> |
| 9 | + |
| 10 | + <h3> |
| 11 | + User Profile |
| 12 | + </h3> |
| 13 | + |
| 14 | + <div ngModelGroup="form.user"> |
| 15 | + |
| 16 | + <p> |
| 17 | + <label> |
| 18 | + <strong>Name:</strong><br /> |
| 19 | + <!-- |
| 20 | + Text-based inputs are the bread-and-butter of template-driven forms. |
| 21 | + You just bind to the view-model with [(ngModel)] and it just works. |
| 22 | + --> |
| 23 | + <input |
| 24 | + type="text" |
| 25 | + name="name" |
| 26 | + [(ngModel)]="form.user.name" |
| 27 | + size="20" |
| 28 | + /> |
| 29 | + </label> |
| 30 | + </p> |
| 31 | + |
| 32 | + <p> |
| 33 | + <label> |
| 34 | + <strong>Bio:</strong><br /> |
| 35 | + <!-- |
| 36 | + Textarea are just a different form of text-based inputs. As such, |
| 37 | + they also work seamlessly with [(ngModel)]. |
| 38 | + --> |
| 39 | + <textarea |
| 40 | + name="bio" |
| 41 | + [(ngModel)]="form.user.bio" |
| 42 | + rows="5" |
| 43 | + cols="70" |
| 44 | + ></textarea> |
| 45 | + </label> |
| 46 | + </p> |
| 47 | + |
| 48 | + </div> |
| 49 | + |
| 50 | + <h3> |
| 51 | + Favorite Movie |
| 52 | + </h3> |
| 53 | + |
| 54 | + <ul> |
| 55 | + <li *ngFor="let movie of movies"> |
| 56 | + <label> |
| 57 | + <!-- |
| 58 | + Unlike a text-based input, Radio controls are a bit different in that |
| 59 | + they have both a value AND a "state" (checked). As such, we need to |
| 60 | + provide two references: the current view-model value (form.favoriteMovie) |
| 61 | + and the value of the control (movie). The RadioControlValueAccessor |
| 62 | + directive overrides the [value] input binding of the native control, |
| 63 | + intercepting it and using it to determine if the radio's NgModel |
| 64 | + value matches the [value] reference. |
| 65 | + --> |
| 66 | + <input |
| 67 | + type="radio" |
| 68 | + name="favoriteMovie" |
| 69 | + [(ngModel)]="form.favoriteMovie" |
| 70 | + [value]="movie" |
| 71 | + /> |
| 72 | + {{ movie.name }} ( {{ movie.releasedAt }} ) |
| 73 | + </label> |
| 74 | + </li> |
| 75 | + <li> |
| 76 | + <label> |
| 77 | + <!-- We can use a [value]="null" option to clear the radio choice. --> |
| 78 | + <input |
| 79 | + type="radio" |
| 80 | + name="favoriteMovie" |
| 81 | + [(ngModel)]="form.favoriteMovie" |
| 82 | + [value]="null" |
| 83 | + /> |
| 84 | + None / Clear |
| 85 | + </label> |
| 86 | + </li> |
| 87 | + </ul> |
| 88 | + |
| 89 | + <h3> |
| 90 | + Favorite Genres |
| 91 | + </h3> |
| 92 | + |
| 93 | + <ul> |
| 94 | + <li *ngFor="let genre of genres"> |
| 95 | + <label> |
| 96 | + <!-- |
| 97 | + The checkbox control is similar to the radio control in that it has |
| 98 | + both a value and a state (checked); but, unlike the RadioControlValueAccessor, |
| 99 | + the CheckboxControlValueAccessor directive deals exclusively with |
| 100 | + Booleans. As such, it doesn't have to override any [value] input |
| 101 | + binding - the NgModel view-model must reference a Boolean, which |
| 102 | + is how the directive drives the [checked] state. |
| 103 | + --> |
| 104 | + <input |
| 105 | + type="checkbox" |
| 106 | + name="favoriteGenre_{{ genre.id }}" |
| 107 | + [(ngModel)]="form.favoriteGenres[ genre.id ]" |
| 108 | + /> |
| 109 | + {{ genre.name }} |
| 110 | + </label> |
| 111 | + </li> |
| 112 | + </ul> |
| 113 | + |
| 114 | + <h3> |
| 115 | + Favorite Snacks |
| 116 | + </h3> |
| 117 | + |
| 118 | + <p> |
| 119 | + <!-- |
| 120 | + Just as with the Radio control, the Select control seeks to match the view- |
| 121 | + model value with a set of possible values. As such, we need to provide two |
| 122 | + references: the current view-model value (form.favoriteSnacks) and the value |
| 123 | + of the option (snackOption). However, unlike the Radio control, the Select |
| 124 | + control uses [ngValue] to provide the option value in its child elements. |
| 125 | + -- |
| 126 | + NOTE: Since we're using "multiple", the selected values are collected in |
| 127 | + an Array. |
| 128 | + --> |
| 129 | + <select |
| 130 | + name="favoriteSnacks" |
| 131 | + [(ngModel)]="form.favoriteSnacks" |
| 132 | + multiple |
| 133 | + size="5"> |
| 134 | + <option *ngFor="let snackOption of snacks" [ngValue]="snackOption"> |
| 135 | + {{ snackOption.name }} |
| 136 | + </option> |
| 137 | + </select> |
| 138 | + </p> |
| 139 | + |
| 140 | + <h3> |
| 141 | + Movie Watching Behavior |
| 142 | + </h3> |
| 143 | + |
| 144 | + <p> |
| 145 | + <!-- |
| 146 | + The single Select works just like the "multiple" Select; except, it doesn't |
| 147 | + gather the options in an Array - it just binds the selected option [ngValue] |
| 148 | + to the view-model (form.watchOption). |
| 149 | + --> |
| 150 | + <select name="watchOption" [(ngModel)]="form.watchOption"> |
| 151 | + <option [ngValue]="null"> |
| 152 | + Please select an option. |
| 153 | + </option> |
| 154 | + <option *ngFor="let option of watchOptions" [ngValue]="option"> |
| 155 | + {{ option.label }} |
| 156 | + </option> |
| 157 | + </select> |
| 158 | + </p> |
| 159 | + |
| 160 | + <p> |
| 161 | + <button type="submit"> |
| 162 | + Submit Form |
| 163 | + </button> |
| 164 | + </p> |
| 165 | + |
| 166 | +</form> |
| 167 | + |
| 168 | + |
| 169 | +<hr /> |
| 170 | + |
| 171 | +<h2> |
| 172 | + Form View-Model Current State |
| 173 | +</h2> |
| 174 | + |
| 175 | +<pre><code>{{ form | json }}</code></pre> |
0 commit comments