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('');
}
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.
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.
AngularJS lets you create dropdown lists based on items in an array, or an object.
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);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With