how to add onchange event on dropdown in angular ?

前端 未结 2 1446
北海茫月
北海茫月 2021-02-05 09:46

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<

相关标签:
2条回答
  • 2021-02-05 10:33

    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);
    }
    
    0 讨论(0)
  • 2021-02-05 10:39

    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);
    }
    
    0 讨论(0)
提交回复
热议问题