0

My question is similar to that question: How to pass 2 parameters to EventEmitter angular 2

i've read the question and the answers, and followd the answer that seem to work for my case but it didnt work in my case.

i need to pass 2 parameter from 2 different Inputs to an EventEmitter. Those 2 parameters would be added to an array in other component and the displayed in a list that will show the ingredient title and its amount.

I can't figure out what's the problam here.

this is component.ts where the EventEmitter is:

and this is the html component from which i recive the 2 parameters:

Let me try to explain my whole procces if it would help you understand and help me out.

first i recive two inputs from 'shoppingListButtons.component.html'

   <div class="container">
<form>
    <div class="form-group col-lg-4 col-md-3">
        <label for="recipeName">{{inputTName}}</label>
        <input type="text" class="form-control" id="recipeName" [(ngModel)]="ingredientTitle" name="title">
    </div>

    <div class="form-group col-lg-2 col-md-3">
        <label for="recipeAmount">{{inputTAmount}}</label>
        <input type="text" class="form-control" id="recipeAmount" [(ngModel)]="ingredientAmount" name="amount">
    </div>

</form>
</div>

<div>
     <button class="btn btn-success" (click)="AddToShoppingList()">{{btnToAdd}}</button>
    <button class="btn btn-danger">{{btnDel}}</button>
    <button class="btn btn-primary" (click)="clear()">{{btnClr}}</button>
</div>

then, these two inputs: 'ingredientTitle' and 'ingredientAmount' supposed to be passed to the EventEmitter that is in 'shoppingListButtons.component.ts'

import { Component, Input, Output, EventEmitter } from '@angular/core'

@Component({
    selector: 'shoppingListButtons',
    templateUrl: './shoppingListButtons.component.html'

    })
    export class ShoppingListButtonComponent {
    @Input() buttonVar: string;
    @Input() buttonNew: string;

    @Input() inputTName: string;
    @Input() inputTAmount: number;
    @Input() btnToAdd: string;
    @Input() btnDel: string;
    @Input() btnClr: string;

    @Input() ingredientTitle: string;
    @Input() ingredientAmount: string;

    @Output() shoppingListChanged: EventEmitter<any> = new EventEmitter();

    AddToShoppingList() {
        this.shoppingListChanged.emit(this.ingredientTitle);
    }
}

After that, the Emitter should pass the parameters to the method: 'AddIngredients' that is in 'shoppingList.component.ts'

export class ShoppingListComponent {

 constructor(private _shoppingListService: ShoppingListService) { }

 ingredientsList = this._shoppingListService.ingredientsShoppingList;

 listTitle: string;
 listAmount: number;

 AddIngredients(listTitle, listAmount) {
   this._shoppingListService.AddIngredients(listTitle, listAmount)
 }

That method is in a seperated service that hold an array in which i want to display it with ngFor in a different html component.

the array:

@Injectable()
export class ShoppingListService {

ingredientsShoppingList: ShoppingListItem[] = [
{Title:"Milk", Amount:3},
{Title:"Cream", Amount: 2}, 
{Title: "Peaches", Amount: 5}]


AddIngredients(listTitle: string, listAmount: number){
  this.ingredientsShoppingList.push({Title: listTitle, Amount: listAmount})
}

GetIngredients(){
    return this.ingredientsShoppingList;
}

}

how i display my list in shoppingList.component.html:

<div class="container">
<h2>Shopping List</h2>
<shoppingListButtons (shoppingListChanged)="AddIngredients($event)" [inputTName]="inputTitleName" [inputTAmount]="inputTitleAmount"
  [btnToAdd]="btnAdd" [btnDel]="btnDelete" [btnClr]="btnClear"></shoppingListButtons>
MetaDude
  • 139
  • 5
  • 14
  • hey, it wont compile if i change it to the way you suggested using 'this.ingredient...' – MetaDude Oct 06 '17 at 15:00
  • Ops sorry it should be this.shoppingListChanged.emit({ ingredientTitle: this.ingredientTitle, ingredientAmount : this.ingredientAmount }); – trungk18 Oct 06 '17 at 15:01
  • So basically because on the view, AddToShoppingList() receive none parameter so that inside the controller all your value will be undefined. You want to get the value inside controller by using this with the corresponding data as well for emitting. Inside emit, you pass an object and an object is a key value pair collection. You have to set the key and value like ingredientTitle: this.ingredientTitle. – trungk18 Oct 06 '17 at 15:04
  • Regardless of your problem, I think you should group all your basic input inside a single object – Mozgor Oct 06 '17 at 15:45
  • @Mozgor ok how to i do that? – MetaDude Oct 06 '17 at 15:47
  • @axlj i'm adding more code of the same program to try and describe my ONLY problam with it. english is not my nativ language and i'm new to angular programming, so its hard for me to describe my problam good enough for you to understand. – MetaDude Oct 06 '17 at 16:13
  • @MetaDude first, declare an interface containing whatever you need as input (usually, those interfaces are declared in specific files). For instance, your interface should be a bunch of number / string. Then, import this interface inside your component, and write your input as : `@Input() public mySweetParameters: MySweetInterface;` Finally all you need is to call your parameters using `this.mySweetParameters.buttonVar` for example. – Mozgor Oct 09 '17 at 09:39

0 Answers0