Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practice for trigger a method by user's input in angular2

I have this scenario: expecting an user's input and use that to _http.get() from server. I have tried using keyup event, but can't manage. Any recommendation is much appreciated.

My template:

<div class="col-md-8">
   <input type="text" class="form-control" [(ngModel)]="workOrder" (keyup)="populate()" placeholder="{{ 'FIELDS.PMS_IFR_WO' | translate }}"/>
</div>

My ts file:

populate(){
  console.log(this.workOrder);
  this._lookup.getIcrData().subscribe(res => {
    console.log(res);
  });
}
like image 714
Claudiu Ungureanu Avatar asked Nov 26 '25 09:11

Claudiu Ungureanu


2 Answers

You are looking for ngModelChange:

<div class="col-md-8">
    <input type="text" class="form-control" [ngModel]="workOrder" (ngModelChange)="populate($event)" placeholder="{{ 'FIELDS.PMS_IFR_WO' | translate }}"/>
</div>

Component

populate(value: any){
  console.log(value);
  this._lookup.getIcrData().subscribe(res => {
        console.log(res);
  });
}
like image 135
Stefan Svrkota Avatar answered Nov 28 '25 03:11

Stefan Svrkota


ngModelChange is usually a good option

<input type="text" class="form-control" 
    [(ngModel)]="workOrder" 
    (ngModelChange)="populate()" 
    placeholder="{{ 'FIELDS.PMS_IFR_WO' | translate }}"/>
like image 36
Günter Zöchbauer Avatar answered Nov 28 '25 03:11

Günter Zöchbauer



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!