| @@ -4,16 +4,24 @@ | |||||
| <div class="row"> | <div class="row"> | ||||
| <div class="col-sm-5 form-group"> | <div class="col-sm-5 form-group"> | ||||
| <label for="name">Name</label> | <label for="name">Name</label> | ||||
| <input type="text" id="name" class="form-control"> | |||||
| <input | |||||
| type="text" | |||||
| id="name" | |||||
| class="form-control" | |||||
| #nameInput> | |||||
| </div> | </div> | ||||
| <div class="col-sm-2 form-group"> | <div class="col-sm-2 form-group"> | ||||
| <label for="amount">Amount</label> | <label for="amount">Amount</label> | ||||
| <input type="text" id="amount" class="form-control"> | |||||
| <input | |||||
| type="number" | |||||
| id="amount" | |||||
| class="form-control" | |||||
| #amountInput> | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| <div class="row"> | <div class="row"> | ||||
| <div class="col-xs-12"> | <div class="col-xs-12"> | ||||
| <button class="btn btn-success" type="submit">Add</button> | |||||
| <button class="btn btn-success" type="submit" (click)="onAddItem()">Add</button> | |||||
| <button class="btn btn-danger" type="button">Delete</button> | <button class="btn btn-danger" type="button">Delete</button> | ||||
| <button class="btn btn-primary" type="button">Clear</button> | <button class="btn btn-primary" type="button">Clear</button> | ||||
| </div> | </div> | ||||
| @@ -1,4 +1,5 @@ | |||||
| import { Component, OnInit } from '@angular/core'; | |||||
| import { Component, OnInit, ViewChild, ElementRef, EventEmitter, Output } from '@angular/core'; | |||||
| import { Ingredient } from '../../shared/ingredient.model'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-shopping-edit', | selector: 'app-shopping-edit', | ||||
| @@ -6,10 +7,18 @@ import { Component, OnInit } from '@angular/core'; | |||||
| styleUrls: ['./shopping-edit.component.css'] | styleUrls: ['./shopping-edit.component.css'] | ||||
| }) | }) | ||||
| export class ShoppingEditComponent implements OnInit { | export class ShoppingEditComponent implements OnInit { | ||||
| @ViewChild('nameInput') nameInputRef: ElementRef; | |||||
| @ViewChild('amountInput') amountInputRef: ElementRef; | |||||
| @Output() ingredientAdded = new EventEmitter<Ingredient>(); | |||||
| constructor() { } | constructor() { } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| } | } | ||||
| onAddItem() { | |||||
| const newIngredient = new Ingredient(this.nameInputRef.nativeElement.value, | |||||
| this.amountInputRef.nativeElement.value); | |||||
| this.ingredientAdded.emit(newIngredient); | |||||
| } | |||||
| } | } | ||||
| @@ -1,6 +1,7 @@ | |||||
| <div class="row"> | <div class="row"> | ||||
| <div class="col-xs-10"> | <div class="col-xs-10"> | ||||
| <app-shopping-edit></app-shopping-edit> | |||||
| <app-shopping-edit | |||||
| (ingredientAdded)="onIngredientAdded($event)"></app-shopping-edit> | |||||
| <hr> | <hr> | ||||
| <ul class="list-group"> | <ul class="list-group"> | ||||
| <a | <a | ||||
| @@ -17,4 +17,8 @@ export class ShoppingListComponent implements OnInit { | |||||
| ngOnInit() { | ngOnInit() { | ||||
| } | } | ||||
| onIngredientAdded(ingredient: Ingredient) { | |||||
| this.ingredients.push(ingredient); | |||||
| } | |||||
| } | } | ||||