'How to pass 2 parameters separately to the EventEmitter to pass them to function in Angular 2?
I have a function that takes 2 parameters
students.component.ts
write(name : string, age : number) : void {
...
}
students.component.html
<app-student [student]="student" (write)="write(**?**)"></app-student>
how can I pass 2 variables from a child component?
student.component.ts
@Output() write = new EventEmitter();
writeStudentInfo(name : string, age : number) {
this.write.emit(**?**);
}
Solution 1:[1]
In this case, you'd either want two emitters, or change your code to be
@Output() write = new EventEmitter();
writeStudentInfo(name : string, age : number) {
this.write.emit({ name, age });
}
and then your host component listener could be
onStudentInfo({name, age}) {
// Do your thing here
}
Solution 2:[2]
@JSmart523 already answered correctly.
Just for your codes, see below,
students.component.ts
write(name : string, age : number) : void {
console.log(name, age);
}
students.component.html
<app-student [student]="student" (write)="write($event.name, $event.age)"></app-student>
student.component.ts
@Output() write = new EventEmitter();
writeStudentInfo(name : string, age : number) {
this.write.emit({ name, age });
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JSmart523 |
| Solution 2 | ByungYong Kang |
