浏览代码

06-80 Adding ingredients

tags/before_ngrx
父节点
当前提交
71e353d9c0
共有 4 个文件被更改,包括 27 次插入5 次删除
  1. +11
    -3
      src/app/shopping-list/shopping-edit/shopping-edit.component.html
  2. +10
    -1
      src/app/shopping-list/shopping-edit/shopping-edit.component.ts
  3. +2
    -1
      src/app/shopping-list/shopping-list.component.html
  4. +4
    -0
      src/app/shopping-list/shopping-list.component.ts

+ 11
- 3
src/app/shopping-list/shopping-edit/shopping-edit.component.html 查看文件

@@ -4,16 +4,24 @@
<div class="row">
<div class="col-sm-5 form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control">
<input
type="text"
id="name"
class="form-control"
#nameInput>
</div>
<div class="col-sm-2 form-group">
<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 class="row">
<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-primary" type="button">Clear</button>
</div>


+ 10
- 1
src/app/shopping-list/shopping-edit/shopping-edit.component.ts 查看文件

@@ -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({
selector: 'app-shopping-edit',
@@ -6,10 +7,18 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./shopping-edit.component.css']
})
export class ShoppingEditComponent implements OnInit {
@ViewChild('nameInput') nameInputRef: ElementRef;
@ViewChild('amountInput') amountInputRef: ElementRef;
@Output() ingredientAdded = new EventEmitter<Ingredient>();

constructor() { }

ngOnInit() {
}

onAddItem() {
const newIngredient = new Ingredient(this.nameInputRef.nativeElement.value,
this.amountInputRef.nativeElement.value);
this.ingredientAdded.emit(newIngredient);
}
}

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

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


+ 4
- 0
src/app/shopping-list/shopping-list.component.ts 查看文件

@@ -17,4 +17,8 @@ export class ShoppingListComponent implements OnInit {
ngOnInit() {
}

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

}

正在加载...
取消
保存