Browse Source

10-108 Handling adding items to shoppinglist

tags/before_ngrx
Nils Dittberner 8 years ago
parent
commit
5f271d124f
5 changed files with 33 additions and 18 deletions
  1. +2
    -1
      src/app/app.module.ts
  2. +4
    -4
      src/app/shopping-list/shopping-edit/shopping-edit.component.ts
  3. +1
    -2
      src/app/shopping-list/shopping-list.component.html
  4. +9
    -10
      src/app/shopping-list/shopping-list.component.ts
  5. +17
    -1
      src/app/shopping-list/shopping-list.service.ts

+ 2
- 1
src/app/app.module.ts View File

@@ -13,6 +13,7 @@ import { RecipeItemComponent } from './recipes/recipe-list/recipe-item/recipe-it
import { ShoppingListComponent } from './shopping-list/shopping-list.component'; import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { ShoppingEditComponent } from './shopping-list/shopping-edit/shopping-edit.component'; import { ShoppingEditComponent } from './shopping-list/shopping-edit/shopping-edit.component';
import { DropdownDirective } from './shared/dropdown.directive'; import { DropdownDirective } from './shared/dropdown.directive';
import { ShoppingListService } from './shopping-list/shopping-list.service';




@NgModule({ @NgModule({
@@ -32,7 +33,7 @@ import { DropdownDirective } from './shared/dropdown.directive';
FormsModule, FormsModule,
HttpModule HttpModule
], ],
providers: [],
providers: [ShoppingListService],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule { }

+ 4
- 4
src/app/shopping-list/shopping-edit/shopping-edit.component.ts View File

@@ -1,5 +1,6 @@
import { Component, OnInit, ViewChild, ElementRef, EventEmitter, Output } from '@angular/core';
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Ingredient } from '../../shared/ingredient.model'; import { Ingredient } from '../../shared/ingredient.model';
import { ShoppingListService } from '../shopping-list.service';


@Component({ @Component({
selector: 'app-shopping-edit', selector: 'app-shopping-edit',
@@ -9,9 +10,8 @@ import { Ingredient } from '../../shared/ingredient.model';
export class ShoppingEditComponent implements OnInit { export class ShoppingEditComponent implements OnInit {
@ViewChild('nameInput') nameInputRef: ElementRef; @ViewChild('nameInput') nameInputRef: ElementRef;
@ViewChild('amountInput') amountInputRef: ElementRef; @ViewChild('amountInput') amountInputRef: ElementRef;
@Output() ingredientAdded = new EventEmitter<Ingredient>();


constructor() { }
constructor(private shoppingListService: ShoppingListService) { }


ngOnInit() { ngOnInit() {
} }
@@ -19,6 +19,6 @@ export class ShoppingEditComponent implements OnInit {
onAddItem() { onAddItem() {
const newIngredient = new Ingredient(this.nameInputRef.nativeElement.value, const newIngredient = new Ingredient(this.nameInputRef.nativeElement.value,
this.amountInputRef.nativeElement.value); this.amountInputRef.nativeElement.value);
this.ingredientAdded.emit(newIngredient);
this.shoppingListService.addIngredient(newIngredient);
} }
} }

+ 1
- 2
src/app/shopping-list/shopping-list.component.html View File

@@ -1,7 +1,6 @@
<div class="row"> <div class="row">
<div class="col-xs-10"> <div class="col-xs-10">
<app-shopping-edit
(ingredientAdded)="onIngredientAdded($event)"></app-shopping-edit>
<app-shopping-edit></app-shopping-edit>
<hr> <hr>
<ul class="list-group"> <ul class="list-group">
<a <a


+ 9
- 10
src/app/shopping-list/shopping-list.component.ts View File

@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Ingredient } from '../shared/ingredient.model'; import { Ingredient } from '../shared/ingredient.model';
import { ShoppingListService } from './shopping-list.service';


@Component({ @Component({
selector: 'app-shopping-list', selector: 'app-shopping-list',
@@ -7,18 +8,16 @@ import { Ingredient } from '../shared/ingredient.model';
styleUrls: ['./shopping-list.component.css'] styleUrls: ['./shopping-list.component.css']
}) })
export class ShoppingListComponent implements OnInit { export class ShoppingListComponent implements OnInit {
ingredients: Ingredient[] = [
new Ingredient('Apples', 5),
new Ingredient('Tomatos', 10)
];
ingredients: Ingredient[];


constructor() { }
constructor(private shoppingListService: ShoppingListService) { }


ngOnInit() { ngOnInit() {
this.ingredients = this.shoppingListService.getIngredients();
this.shoppingListService.ingredientsChanged.subscribe(
(ingredients: Ingredient[]) => {
this.ingredients = ingredients;
}
);
} }

onIngredientAdded(ingredient: Ingredient) {
this.ingredients.push(ingredient);
}

} }

+ 17
- 1
src/app/shopping-list/shopping-list.service.ts View File

@@ -1,3 +1,19 @@
import { Ingredient } from "../shared/ingredient.model";
import { EventEmitter } from "@angular/core";

export class ShoppingListService { export class ShoppingListService {
ingredientsChanged = new EventEmitter<Ingredient[]>();
private ingredients: Ingredient[] = [
new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10)
];

getIngredients() {
return this.ingredients.slice();
}

addIngredient(ingredient: Ingredient) {
this.ingredients.push(ingredient);
this.ingredientsChanged.emit(this.ingredients.slice());
}
} }

Loading…
Cancel
Save