Angular 2 select option (dropdown) - how to get the value on change so it can be used in a function?

前端 未结 6 913
小蘑菇
小蘑菇 2020-12-13 17:49

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

6条回答
  •  春和景丽
    2020-12-13 18:20

    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

提交回复
热议问题