| @@ -1,5 +1,6 @@ | |||||
| import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core'; | |||||
| import { Component, OnInit, Input } from '@angular/core'; | |||||
| import { Recipe } from '../../recipe.model'; | import { Recipe } from '../../recipe.model'; | ||||
| import { RecipeService } from '../../recipe.service'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-recipe-item', | selector: 'app-recipe-item', | ||||
| @@ -8,15 +9,14 @@ import { Recipe } from '../../recipe.model'; | |||||
| }) | }) | ||||
| export class RecipeItemComponent implements OnInit { | export class RecipeItemComponent implements OnInit { | ||||
| @Input() recipe: Recipe; | @Input() recipe: Recipe; | ||||
| @Output() recipeSelected = new EventEmitter<void>(); | |||||
| constructor() { } | |||||
| constructor(private recipeService: RecipeService) { } | |||||
| ngOnInit() { | ngOnInit() { | ||||
| } | } | ||||
| onSelected() { | onSelected() { | ||||
| this.recipeSelected.emit(); | |||||
| this.recipeService.recipeSelected.emit(this.recipe); | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,4 +1,4 @@ | |||||
| import { Component, OnInit, EventEmitter, Output } from '@angular/core'; | |||||
| import { Component, OnInit } from '@angular/core'; | |||||
| import { Recipe } from '../recipe.model'; | import { Recipe } from '../recipe.model'; | ||||
| import { RecipeService } from '../recipe.service'; | import { RecipeService } from '../recipe.service'; | ||||
| @@ -9,16 +9,10 @@ import { RecipeService } from '../recipe.service'; | |||||
| }) | }) | ||||
| export class RecipeListComponent implements OnInit { | export class RecipeListComponent implements OnInit { | ||||
| recipes: Recipe[]; | recipes: Recipe[]; | ||||
| @Output() recipeWasSelected = new EventEmitter<Recipe>(); | |||||
| constructor(private recipeService: RecipeService) { } | constructor(private recipeService: RecipeService) { } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.recipes = this.recipeService.getRecipes(); | this.recipes = this.recipeService.getRecipes(); | ||||
| } | } | ||||
| onRecipeSelected(recipe: Recipe) { | |||||
| this.recipeWasSelected.emit(recipe); | |||||
| } | |||||
| } | } | ||||
| @@ -1,6 +1,9 @@ | |||||
| import { Recipe } from "./recipe.model"; | import { Recipe } from "./recipe.model"; | ||||
| import { EventEmitter } from "@angular/core"; | |||||
| export class RecipeService { | export class RecipeService { | ||||
| recipeSelected = new EventEmitter<Recipe>(); | |||||
| private recipes: Recipe[] = [ | private recipes: Recipe[] = [ | ||||
| new Recipe('A Test Recipe', 'This is simlpy a test', 'http://maxpixel.freegreatpicture.com/static/photo/1x/Recipe-Soup-Noodle-Curried-Spicy-Chicken-Khaosoi-2344152.jpg'), | new Recipe('A Test Recipe', 'This is simlpy a test', 'http://maxpixel.freegreatpicture.com/static/photo/1x/Recipe-Soup-Noodle-Curried-Spicy-Chicken-Khaosoi-2344152.jpg'), | ||||
| new Recipe('Another Test Recipe', 'This is simlpy a test', 'http://maxpixel.freegreatpicture.com/static/photo/1x/Recipe-Soup-Noodle-Curried-Spicy-Chicken-Khaosoi-2344152.jpg') | new Recipe('Another Test Recipe', 'This is simlpy a test', 'http://maxpixel.freegreatpicture.com/static/photo/1x/Recipe-Soup-Noodle-Curried-Spicy-Chicken-Khaosoi-2344152.jpg') | ||||
| @@ -1,7 +1,6 @@ | |||||
| <div class="row"> | <div class="row"> | ||||
| <div class="col-md-5"> | <div class="col-md-5"> | ||||
| <app-recipe-list | |||||
| (recipeWasSelected)="selectedRecipe = $event"></app-recipe-list> | |||||
| <app-recipe-list></app-recipe-list> | |||||
| </div> | </div> | ||||
| <div class="col-md-7"> | <div class="col-md-7"> | ||||
| <app-recipe-detail | <app-recipe-detail | ||||
| @@ -12,9 +12,14 @@ import { RecipeService } from './recipe.service'; | |||||
| export class RecipesComponent implements OnInit { | export class RecipesComponent implements OnInit { | ||||
| selectedRecipe: Recipe; | selectedRecipe: Recipe; | ||||
| constructor() { } | |||||
| constructor(private recipeService: RecipeService) { } | |||||
| ngOnInit() { | ngOnInit() { | ||||
| this.recipeService.recipeSelected.subscribe( | |||||
| (recipe: Recipe) => { | |||||
| this.selectedRecipe = recipe; | |||||
| } | |||||
| ); | |||||
| } | } | ||||
| } | } | ||||