Home > Software engineering >  How to know which p-dropdown has triggered the event
How to know which p-dropdown has triggered the event

Time:01-21

I've two p-dropdowns inside a formGroup and both are calling the same generic method.

<!-- Fruit List -->
<p-dropdown
  (onChange)="callMethod($event)"
  ...
>
</p-dropdown>

<!-- Vegetable List -->
<p-dropdown
  (onChange)="callMethod($event)"
  ...
>
</p-dropdown>

Notice that both are calling the same callMethod method. How can I distinguish between these two dropdowns inside my method. I didn't get a single content for the same on the internet. Please help.

CodePudding user response:

The simplest would be to add another parameter to callMethod and pass the list name as an argument.

callMethod($event, listType: string) {

 switch(listType){
   case 'fruitList': {
     ...
     break;
   }
   case 'vegetableList': {
     ...
     break;
   }
 }
}
<!-- Fruit List -->
<p-dropdown
  (onChange)="callMethod($event, 'fruitList')"
  ...
>
</p-dropdown>

<!-- Vegetable List -->
<p-dropdown
  (onChange)="callMethod($event, 'vegetableList')"
  ...
>
</p-dropdown>
  • Related