Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to know which p-dropdown has triggered the event

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading