Call the method when the value is changed using Angular 2

How can I call a method when changing a value using Angular 2?

 <select [(ngModel)]="searchMonster">
     <option>36</option>
     <option>37</option>
 </select>

I tried using ng-change but was unsuccessful.

+4
source share
2 answers

[(ngModel)] , angular. / . [(ngModel)] [ngModel] (ngModelChange) , onChange() , , : -

<select [(ngModel)]="selectedDevice" #device (change)="onChange(device.value)">
    <option *ngFor="#i of devices">{{i}}</option>
</select>

onChange(deviceValue) {
    console.log(deviceValue);
}

this

+6

ngModelChange. .

<select
  [ngModel]="searchMonster"
  (ngModelChange)="yourMethod($event)">
     <option>36</option>
     <option>37</option>
</select>

, <input>, <select>

<select #option
  [ngModel]="searchMonster"
  (ngModelChange)="yourMethod(option.value)">
     <option>36</option>
     <option>37</option>
</select>    
+6

Source: https://habr.com/ru/post/1621365/


All Articles