Professional Documents
Culture Documents
Allowing The User To Add Ingredients To The Shopping List
Allowing The User To Add Ingredients To The Shopping List
Allowing The User To Add Ingredients To The Shopping List
1. In shopping-edit.component.html
In our input Element, we have added the below local references
#nameInput
#amountInput
2. In shopping-edit.component.ts
*******************
onAddItem() {
const ingName = this.nameInputRef.nativeElement.value;
const ingAmount = this.amountInputRef.nativeElement.value;
const newIngredient = new Ingredient(ingName, ingAmount);
this.ingredientAdded.emit(newIngredient);
3. in shopping-list.component.html
we listen to our (ingredientAdded) event, we called the function here and pass
$event
so we can get the event thrown by our event.
**************
<div class="row">
<div class="col-md-12">
<app-shopping-edit (ingredientAdded)="onIngredientAdded($event)"></app-
shopping-edit>
<hr>
<ul class="list-group-item">
<a class="list-group-item" style="cursor:pointer" *ngFor="let
ingredient of ingredients">
{{ingredient.name}} ({{ingredient.amount}})
</a>
</ul>
</div>
</div>
4. in our shopping-list.component.ts
onIngredientAdded(ingredient: Ingredient) {
this.ingredients.push(ingredient);
}