I am trying to build a drop down with a few values.
However, on selecting a value, I want to make an API call that takes an id.
In my component.ts, I have an
You need to use an Angular form directive on the select
. You can do that with ngModel
. For example
@Component({
selector: 'my-app',
template: `
Select demo
`
})
class App {
cities = [{'name': 'SF'}, {'name': 'NYC'}, {'name': 'Buffalo'}];
selectedCity = this.cities[1];
onChange(city) {
alert(city.name);
}
}
The (ngModelChange)
event listener emits events when the selected value changes. This is where you can hookup your callback.
Note you will need to make sure you have imported the FormsModule
into the application.
Here is a Plunker