Created
July 2, 2017 11:21
-
-
Save Armenvardanyan95/73a80a0d1c518adadf3caa5ed2a8e30a to your computer and use it in GitHub Desktop.
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
| interface Movie { | |
| id: number; | |
| title: string; | |
| } | |
| @Component({ | |
| selector: 'app-some-component-with-form', | |
| template: `...` // our form is here | |
| }) | |
| export class SomeComponentWithForm { | |
| public form: FormGroup; | |
| public movies: Array<Movie> | |
| constructor(private formBuilder: FormBuilder){ | |
| this.form = formBuilder.group({ | |
| firstName: ['', Validators.required], | |
| lastName: ['', Validators.required], | |
| age: ['', Validators.max(120)], | |
| favoriteMovies: [[]], /* | |
| we'll have a multiselect dropdown | |
| in our template to select favorite movies | |
| */ | |
| }); | |
| } | |
| public onSubmit(values){ | |
| /* | |
| 'values' is actually a form value, which represents a user | |
| but imagine our API does not expect as to send a list of movie | |
| objects, just a list of id-s, so we have to map the values | |
| */ | |
| values.favouriteMovies = values.favouriteMovies.map((movie: Movie) => movie.id); | |
| // then we will send the user data to the server using some service | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment