Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add onchange event on dropdown in angular ?

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('');
}
like image 447
user5711656 Avatar asked Apr 08 '18 06:04

user5711656


People also ask

What event is generated when a dropdown value is changed in angular?

Here, i will give you very simple example to getting selected option value by change event with reactive form. here we will create one website list dropdown and if you choose anyone then it will by print console selected value on change event. we created changeWebsite() that will call on change of dropdown value.

What is Onchange in angular?

Angular 2.0 has two-way data binding. Any changes to the input field will be reflected immediately on the UI and vice versa. Onchange is a property of an input element in Angular 2 that specifies what should happen when the user types into it or selects a value from its dropdown list.

What is dropdown in angular?

AngularJS lets you create dropdown lists based on items in an array, or an object.


2 Answers

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);
}
like image 141
Nour Avatar answered Oct 13 '22 20:10

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);
}
like image 27
Klodian Avatar answered Oct 13 '22 20:10

Klodian