浏览代码

14-166 Improving observables and use subjects, also cleanup

tags/before_ngrx
Nils Dittberner 8 年前
父节点
当前提交
17daebad8d
共有 6 个文件被更改,包括 17 次插入19 次删除
  1. +1
    -1
      src/app/app.component.html
  2. +1
    -1
      src/app/header/header.component.html
  3. +0
    -2
      src/app/recipes/recipe.service.ts
  4. +2
    -9
      src/app/recipes/recipes.component.ts
  5. +9
    -2
      src/app/shopping-list/shopping-list.component.ts
  6. +4
    -4
      src/app/shopping-list/shopping-list.service.ts

+ 1
- 1
src/app/app.component.html 查看文件

@@ -1,4 +1,4 @@
<app-header (featureSelected)="onNavigate($event)"></app-header>
<app-header></app-header>
<div class="container">
<div class="row">
<div class="col-md-12">


+ 1
- 1
src/app/header/header.component.html 查看文件

@@ -1,7 +1,7 @@
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a href="#" class="navbar-brand">Recipe Book</a>
<a routerLink="/" class="navbar-brand">Recipe Book</a>
</div>

<div class="navbar-default">


+ 0
- 2
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<Recipe>();

private recipes: Recipe[] = [
new Recipe(
'A Test Recipe',


+ 2
- 9
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;
}
);
}

}

+ 9
- 2
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();
}
}

+ 4
- 4
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<Ingredient[]>();
ingredientsChanged = new Subject<Ingredient[]>();
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());
}
}

正在加载...
取消
保存