浏览代码

16-215 Adding and editing recipes

tags/before_ngrx
Nils Dittberner 8 年前
父节点
当前提交
aa13a3f600
共有 5 个文件被更改,包括 83 次插入22 次删除
  1. +4
    -0
      src/app/recipes/recipe-edit/recipe-edit.component.css
  2. +27
    -17
      src/app/recipes/recipe-edit/recipe-edit.component.html
  3. +35
    -5
      src/app/recipes/recipe-edit/recipe-edit.component.ts
  4. +5
    -0
      src/app/recipes/recipe-list/recipe-list.component.ts
  5. +12
    -0
      src/app/recipes/recipe.service.ts

+ 4
- 0
src/app/recipes/recipe-edit/recipe-edit.component.css 查看文件

@@ -0,0 +1,4 @@
input.ng-invalid.ng-touched,
textare.ng-invalid.ng-touched {
border: 1px solid red;
}

+ 27
- 17
src/app/recipes/recipe-edit/recipe-edit.component.html 查看文件

@@ -3,7 +3,7 @@
<form [formGroup]="recipeForm" (ngSubmit)="onSubmit()">
<div class="row">
<div class="col-xs-12">
<button type="submit" class="btn btn-success">Save</button>
<button type="submit" class="btn btn-success" [disabled]="!recipeForm.valid">Save</button>
<button type="button" class="btn btn-danger">Cancel</button>
</div>
</div>
@@ -56,22 +56,32 @@
</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 class="col-xs-12" formArrayName="ingredients">
<div
class="row"
*ngFor="let ingredientCtrl of recipeForm.get('ingredients').controls; let i = index"
[formGroupName]="i"
style="margin-top: 10px;">
<div class="col-xs-8">
<input
type="text"
class="form-control"
formControlName="name">
</div>
<div class="col-xs-2">
<input
type="number"
class="form-control"
formControlName="amount">
</div>
<div class="col-xs-2">
<button class="btn btn-danger">x</button>
</div>
</div>
<hr>
<div class="row">
<div class="col-xs-12">
<button type="button" class="btn btn-success" (click)="onAddIngredient()">Add Ingredient</button>
</div>
</div>
</div>


+ 35
- 5
src/app/recipes/recipe-edit/recipe-edit.component.ts 查看文件

@@ -1,7 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { FormGroup, FormControl } from '@angular/forms';
import { FormArray, FormGroup, FormControl, Validators } from '@angular/forms';
import { RecipeService } from '../recipe.service';
import { Recipe } from '../recipe.model';

@Component({
selector: 'app-recipe-edit',
@@ -27,25 +28,54 @@ export class RecipeEditComponent implements OnInit {
}

onSubmit() {
console.log(this.recipeForm);
/* const newRecipe = new Recipe(this.recipeForm.value['name'],
this.recipeForm.valid['description'],
this.recipeForm.valid['imagePath'],
this.recipeForm.value['ingredients']); */
if (this.editMode) {
this.recipeService.updateRecpe(this.id, this.recipeForm.value);
} else {
this.recipeService.addRecipe(this.recipeForm.value);
}
}

onAddIngredient() {
(<FormArray>this.recipeForm.get('ingredients')).push(
new FormGroup({
'name': new FormControl(null, Validators.required),
'amount': new FormControl(null, [Validators.required, Validators.pattern(/^[1-9]+[0-9]*$/)])
})
)
}

private initForm() {
let recipeName = '';
let recipeImagePath = '';
let recipeDescription = '';
let recipeIngredients = new FormArray([]);

if (this.editMode) {
const recipe = this.recipeService.getRecipe(this.id);
recipeName = recipe.name;
recipeImagePath = recipe.imagePath;
recipeDescription = recipe.description;
if (recipe['ingredients']) {
for (let ingredient of recipe.ingredients) {
recipeIngredients.push(
new FormGroup({
'name': new FormControl(ingredient.name, Validators.required),
'amount': new FormControl(ingredient.amount, [Validators.required, Validators.pattern(/^[1-9]+[0-9]*$/)])
})
);
}
}
}

this.recipeForm = new FormGroup({
'name': new FormControl(recipeName),
'imagePath': new FormControl(recipeImagePath),
'description': new FormControl(recipeDescription)
'name': new FormControl(recipeName, Validators.required),
'imagePath': new FormControl(recipeImagePath, Validators.required),
'description': new FormControl(recipeDescription, Validators.required),
'ingredients': recipeIngredients
});
}
}

+ 5
- 0
src/app/recipes/recipe-list/recipe-list.component.ts 查看文件

@@ -18,6 +18,11 @@ export class RecipeListComponent implements OnInit {
private route: ActivatedRoute) { }

ngOnInit() {
this.recipeService.recipesChanged.subscribe(
(recipes: Recipe[]) => {
this.recipes = recipes;
}
);
this.recipes = this.recipeService.getRecipes();
}



+ 12
- 0
src/app/recipes/recipe.service.ts 查看文件

@@ -2,9 +2,11 @@ import { Recipe } from "./recipe.model";
import { EventEmitter, Injectable } from "@angular/core";
import { Ingredient } from "../shared/ingredient.model";
import { ShoppingListService } from "../shopping-list/shopping-list.service";
import { Subject } from "rxjs/Subject";

@Injectable()
export class RecipeService {
recipesChanged = new Subject<Recipe[]>();
private recipes: Recipe[] = [
new Recipe(
'A Test Recipe',
@@ -39,4 +41,14 @@ export class RecipeService {
addIngredientsToShoppingList(ingredients: Ingredient[]) {
this.shoppingListService.addIngredients(ingredients);
}

addRecipe(recipe: Recipe) {
this.recipes.push(recipe);
this.recipesChanged.next(this.recipes.slice());
}

updateRecpe(index: number, newRecipe: Recipe) {
this.recipes[index] = newRecipe;
this.recipesChanged.next(this.recipes.slice());
}
}

正在加载...
取消
保存