0

I have same listener for different inputs.

When listener is fired, I can't get who generates the event.

<form #f="ngForm">
<mat-card-content>
  <mat-input-container>
    <input matInput [(ngModel)]="id" name="id" placeholder="ID" (ngModelChange)="inputChanged()" />
  </mat-input-container>
  <mat-input-container>
    <input matInput [(ngModel)]="name" name="name" placeholder="NAME" (ngModelChange)="inputChanged()" />
  </mat-input-container>
</mat-card-content>
</form>

//ts

inputChanged():void{
  //how to know who generates
}
cucuru
  • 3,456
  • 8
  • 40
  • 74

2 Answers2

1

Just do that:

(ngModelChange)="inputChanged('one')"

inputChanged(name: string):void{
  console.log(name);
}

And put some logic inside inputChanged

Verri
  • 1,580
  • 19
  • 22
0

You can use two different functions and if they need to do same thing, then they can call a shared function later.

<input matInput [(ngModel)]="id" name="id" placeholder="ID" (ngModelChange)="onIdChange($event)"/>

<input matInput [(ngModel)]="name" name="name" placeholder="NAME" (ngModelChange)="onNameChange($event)" />

In component.ts

onIdChange(event) {
   // some logic
}

onNameChange(event) {
   // some logic
}
Bunyamin Coskuner
  • 8,719
  • 1
  • 28
  • 48