diff --git a/src/app/recipes/recipe.service.ts b/src/app/recipes/recipe.service.ts
index ee376af..9f02c1c 100644
--- a/src/app/recipes/recipe.service.ts
+++ b/src/app/recipes/recipe.service.ts
@@ -5,8 +5,6 @@ import { ShoppingListService } from "../shopping-list/shopping-list.service";
@Injectable()
export class RecipeService {
- recipeSelected = new EventEmitter();
-
private recipes: Recipe[] = [
new Recipe(
'A Test Recipe',
diff --git a/src/app/recipes/recipes.component.ts b/src/app/recipes/recipes.component.ts
index d141413..e63108b 100644
--- a/src/app/recipes/recipes.component.ts
+++ b/src/app/recipes/recipes.component.ts
@@ -1,6 +1,5 @@
import { Component, OnInit } from '@angular/core';
-import { Recipe } from './recipe.model';
import { RecipeService } from './recipe.service';
@Component({
@@ -10,16 +9,10 @@ import { RecipeService } from './recipe.service';
providers: [RecipeService]
})
export class RecipesComponent implements OnInit {
- selectedRecipe: Recipe;
-
- constructor(private recipeService: RecipeService) { }
+
+ constructor() { }
ngOnInit() {
- this.recipeService.recipeSelected.subscribe(
- (recipe: Recipe) => {
- this.selectedRecipe = recipe;
- }
- );
}
}
diff --git a/src/app/shopping-list/shopping-list.component.ts b/src/app/shopping-list/shopping-list.component.ts
index 1ca7413..11a5599 100644
--- a/src/app/shopping-list/shopping-list.component.ts
+++ b/src/app/shopping-list/shopping-list.component.ts
@@ -1,23 +1,30 @@
import { Component, OnInit } from '@angular/core';
import { Ingredient } from '../shared/ingredient.model';
import { ShoppingListService } from './shopping-list.service';
+import { Subscription } from 'rxjs/Subscription';
+import { OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
selector: 'app-shopping-list',
templateUrl: './shopping-list.component.html',
styleUrls: ['./shopping-list.component.css']
})
-export class ShoppingListComponent implements OnInit {
+export class ShoppingListComponent implements OnInit, OnDestroy {
ingredients: Ingredient[];
+ private subscription: Subscription
constructor(private shoppingListService: ShoppingListService) { }
ngOnInit() {
this.ingredients = this.shoppingListService.getIngredients();
- this.shoppingListService.ingredientsChanged.subscribe(
+ this.subscription = this.shoppingListService.ingredientsChanged.subscribe(
(ingredients: Ingredient[]) => {
this.ingredients = ingredients;
}
);
}
+
+ ngOnDestroy() {
+ this.subscription.unsubscribe();
+ }
}
diff --git a/src/app/shopping-list/shopping-list.service.ts b/src/app/shopping-list/shopping-list.service.ts
index fa174ee..c30a2f6 100644
--- a/src/app/shopping-list/shopping-list.service.ts
+++ b/src/app/shopping-list/shopping-list.service.ts
@@ -1,8 +1,8 @@
import { Ingredient } from "../shared/ingredient.model";
-import { EventEmitter } from "@angular/core";
+import { Subject } from "rxjs/Subject";
export class ShoppingListService {
- ingredientsChanged = new EventEmitter();
+ ingredientsChanged = new Subject();
private ingredients: Ingredient[] = [
new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10)
@@ -14,11 +14,11 @@ export class ShoppingListService {
addIngredient(ingredient: Ingredient) {
this.ingredients.push(ingredient);
- this.ingredientsChanged.emit(this.ingredients.slice());
+ this.ingredientsChanged.next(this.ingredients.slice());
}
addIngredients(ingredients: Ingredient[]) {
this.ingredients.push(...ingredients);
- this.ingredientsChanged.emit(this.ingredients.slice());
+ this.ingredientsChanged.next(this.ingredients.slice());
}
}
\ No newline at end of file