Source:  Twitter logo

could you please tell me how to add onchange event on dropdown in angular ? I made a simple demo in which initially I fetch bank names and show in drop down . Now I want to add on change event on that dropdown .In other words I want to which bank user select . Using that bank name I want to get state names

here is my code https://stackblitz.com/edit/angular-batt5c

<select class='select-option' 
        (ngModelChange)='onOptionsSelected($event)'>
    <option class='option' 
    *ngFor='let option of dropDownData' 
    [value]="option.seo_val">{{option.text_val}}</option>
</select>

onOptionsSelected(){
  console.log('===');
  // send selected value
  this.seletedValue.emit('');
}

Use (change) event instead of (ngModelChange).

<select class='select-option'
    #mySelect
    (change)='onOptionsSelected(mySelect.value)'>
   <option class='option' 
   *ngFor='let option of dropDownData' 
   [value]="option.seo_val">{{option.text_val}}</option>
</select>

In typescript file:

onOptionsSelected(value:string){
     console.log("the selected value is " + value);
}
63 users liked answer #0dislike answer #063
Nour profile pic
Nour

try this.

<select class='select-option' [(ngModel)]="selected"
     (change)='onOptionsSelected($event)'>
   <option class='option'  *ngFor='let option of dropDownData' 
   [value]="option.seo_val">{{option.text_val}}</option>
</select>

public onOptionsSelected(event) {
   const value = event.target.value;
   this.selected = value;
   console.log(value);
}
23 users liked answer #1dislike answer #123
Klodian profile pic
Klodian

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).