2

I am trying to fire an event when input value changes. I am using ngModel with ngModelChanges. But its not working

HTML

<input type="number"
[(ngModel)]='myModel' (change)="myModelChange()" >

<button (click)='changeModel()'>Change</button>

Typescipt

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  myModel: number;
  ngOnInit(){
    this.myModel =100;
  }
  changeModel() {
    this.myModel = Math.floor(Math.random() * 100);  
  }
  myModelChange() {
    console.log('changed');
  }
}

Example : Code here

Is it it possible to do this with ngModelChanges. If not then is there any other way?

4

0 回答 0