Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How pass 2 parameters to EventEmitter in Angular?

I have in my component an EventEmitter but I can't compile it because it throws the error:

Supplied parameters do not match any signature of call target

My component:

@Output() addModel = new EventEmitter<any>();

saveModel($event, make, name) {
  this.addModel.emit(make, name);
}

If I delete one of the parameters in this.addModel.emit() it works but is it possible to pass 2 parameters to EventEmitter and how?

like image 522
LorenzoBerti Avatar asked Dec 06 '16 19:12

LorenzoBerti


People also ask

How do you pass two parameters in EventEmitter?

For passing the parameters, we will wrap all the parameters inside the curly brackets (this will combine them as a single object) and pass it to the emit method. To receive the parameters in the parent component, we will make a similar type of object and update its value with that of the received object.

What is @input and @output in Angular?

@Input() and @Output() allow Angular to share data between the parent context and child directives or components. An @Input() property is writable while an @Output() property is observable.

How do event emitters work Angular?

🎊 Event Emitters in Angular 🎊 Data flows into your component via property bindings and flows out of your component through event bindings. If you want your component to notify his parent about something you can use the Output decorator with EventEmitter to create a custom event.

Is EventEmitter observable in Angular?

EventEmitter is used in the directives and components to emit custom events either synchronously or asynchronously. Since EventEmitter class extends RxJS subject class, this means it is observable and can be multicasted to many observers. In Angular 1 Http was a promise based service.


4 Answers

If you look at the EventEmitter emit method @ angular.io, it can only take a single parameter of type T

emit(value?: T)

Since only a single parameter is allowed, consider passing it as an object in emit method.

In the snippet below, make & name variables are holding their respective values:

this.addModel.emit({make: make, name: name});
//shorthand is below
this.addModel.emit({make, name});
like image 191
Pankaj Parkar Avatar answered Oct 10 '22 14:10

Pankaj Parkar


Another option to strongly type it is as follows:

@Output addModel = new EventEmitter<{make: string, name: string}>();

you can then emit it like @Pankaj-Parkar shows

this.addModel.emit({make, name});
or
this.addModel.emit({make: 'honda', name: 'civic'});

You now have strong typing instead of using object or any.

like image 40
Andy Danger Gagne Avatar answered Oct 10 '22 14:10

Andy Danger Gagne


I fixed it by making

EventEmitter<object>();

Then I was able to pass an object such as:

this.MyOutputVariable.emit({ name: 'jack', age: '12' });

And it worked.

like image 28
Adham Amiin Avatar answered Oct 10 '22 16:10

Adham Amiin


I know this is an old Question for me I would create an interface and send it as an object where I can have my code more organized

 export interface addModelArgs{
      make:string,
      name:string
    }
@Output() addModel = new EventEmitter<addModelArgs>();

and call it as following

    this.addModel.emit({make: 'honda', name: 'civic'});
or 
    let savParamters:addModelArgs={make: 'honda', name: 'civic'};

    this.addModel.emit(savParamters);
like image 43
khaled Dehia Avatar answered Oct 10 '22 14:10

khaled Dehia