| @@ -1,6 +1,6 @@ | |||
| import { BrowserModule } from '@angular/platform-browser'; | |||
| import { NgModule } from '@angular/core'; | |||
| import { FormsModule } from '@angular/forms'; | |||
| import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | |||
| import { HttpModule } from '@angular/http'; | |||
| @@ -35,7 +35,8 @@ import { RecipeEditComponent } from './recipes/recipe-edit/recipe-edit.component | |||
| ], | |||
| imports: [ | |||
| BrowserModule, | |||
| FormsModule, | |||
| FormsModule, | |||
| ReactiveFormsModule, | |||
| HttpModule, | |||
| AppRoutingModule | |||
| ], | |||
| @@ -1,3 +1,81 @@ | |||
| <p> | |||
| recipe-edit works! | |||
| </p> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <form [formGroup]="recipeForm" (ngSubmit)="onSubmit()"> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <button type="submit" class="btn btn-success">Save</button> | |||
| <button type="button" class="btn btn-danger">Cancel</button> | |||
| </div> | |||
| </div> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <div class="form-group"> | |||
| <label for="name">Name</label> | |||
| <input | |||
| type="text" | |||
| name="name" | |||
| id="name" | |||
| formControlName="name" | |||
| class="form-control"> | |||
| </div> | |||
| </div> | |||
| </div> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <div class="form-group"> | |||
| <label for="imagePath">Image URL</label> | |||
| <input | |||
| type="text" | |||
| name="imagePath" | |||
| id="imagePath" | |||
| formControlName="imagePath" | |||
| class="form-control"> | |||
| </div> | |||
| </div> | |||
| </div> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <img | |||
| src="" | |||
| alt="" | |||
| class="img-responsive"> | |||
| </div> | |||
| </div> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <div class="form-group"> | |||
| <label for="description">Description</label> | |||
| <textarea | |||
| type="text" | |||
| name="description" | |||
| id="description" | |||
| formControlName="description" | |||
| class="form-control" | |||
| rows="6"></textarea> | |||
| </div> | |||
| </div> | |||
| </div> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <div class="form-group"> | |||
| <div class="row"> | |||
| <div class="col-xs-8"> | |||
| <input | |||
| type="text" | |||
| class="form-control"> | |||
| </div> | |||
| <div class="col-xs-2"> | |||
| <input | |||
| type="text" | |||
| class="form-control"> | |||
| </div> | |||
| <div class="col-xs-2"> | |||
| <button class="btn btn-danger">x</button> | |||
| </div> | |||
| </div> | |||
| </div> | |||
| </div> | |||
| </div> | |||
| </form> | |||
| </div> | |||
| </div> | |||
| @@ -1,5 +1,7 @@ | |||
| import { Component, OnInit } from '@angular/core'; | |||
| import { ActivatedRoute, Params } from '@angular/router'; | |||
| import { FormGroup, FormControl } from '@angular/forms'; | |||
| import { RecipeService } from '../recipe.service'; | |||
| @Component({ | |||
| selector: 'app-recipe-edit', | |||
| @@ -9,16 +11,41 @@ import { ActivatedRoute, Params } from '@angular/router'; | |||
| export class RecipeEditComponent implements OnInit { | |||
| id: number; | |||
| editMode: boolean = false; | |||
| recipeForm: FormGroup | |||
| constructor(private route: ActivatedRoute) { } | |||
| constructor(private route: ActivatedRoute, | |||
| private recipeService: RecipeService) { } | |||
| ngOnInit() { | |||
| this.route.params.subscribe( | |||
| (params: Params) => { | |||
| this.id = +params['id']; | |||
| this.editMode = params['id'] != null; | |||
| this.initForm(); | |||
| } | |||
| ); | |||
| } | |||
| onSubmit() { | |||
| console.log(this.recipeForm); | |||
| } | |||
| private initForm() { | |||
| let recipeName = ''; | |||
| let recipeImagePath = ''; | |||
| let recipeDescription = ''; | |||
| if (this.editMode) { | |||
| const recipe = this.recipeService.getRecipe(this.id); | |||
| recipeName = recipe.name; | |||
| recipeImagePath = recipe.imagePath; | |||
| recipeDescription = recipe.description; | |||
| } | |||
| this.recipeForm = new FormGroup({ | |||
| 'name': new FormControl(recipeName), | |||
| 'imagePath': new FormControl(recipeImagePath), | |||
| 'description': new FormControl(recipeDescription) | |||
| }); | |||
| } | |||
| } | |||