Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a function every time when I select a new option in Angular 2?

I am using Angular 2 (TypeScript).

How do you trigger a function, such as onChange(), whenever when I select a new option?

When I searched online, all I found was information about ng-change in Angular 1. (https://docs.angularjs.org/api/ng/directive/ngChange)

<select [(ng-model)]="selectedDevices">
    <option *ng-for="#i of devices">{{i}}</option>
</select>

Thanks

like image 368
Hongbo Miao Avatar asked Nov 26 '25 21:11

Hongbo Miao


1 Answers

Since angular2 ties directly into DOM events, you can just listen to the change event:

 <select [(ng-model)]="selectedDevices" (change)="changeHandler($event)">

And then in your component:

 public changeHandler($event) {
   // do something here
 }
like image 53
Brocco Avatar answered Nov 29 '25 15:11

Brocco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!