javascript - Angular 2 : trying to update ngModel with onModelChange inside the controller -


i'm trying update ngmodel inside onmodelchange

i know can :

<input [ngmodel]="myvalue" (ngmodelchange)="myvalue = $event" /> 

but not i'm looking for. need validation before updating myvalue

here code :

<input mdinput type="number"         [ngmodel]="tax.refundamount"         (ngmodelchange)="onchange($event, tax.paidamount)" /> 

and in controller :

onchange($event, maxvalue) {     if ($event > maxvalue) {         $event = maxvalue;     } } 

but ngmodel not updated :(

how can update model inside controller ? the input shown here inside ngfor loop cannot access model directly

edit:

i tried [(ngmodel)] , (change) there timing problem when emiting event parent controller

update 1

i want inside component class after updating model, emitting event or computing values. therefore cannot return value , update model in template.

try this?

<input mdinput type="number"                 [ngmodel]="tax.refundamount"                 (ngmodelchange)="tax.refundamount = onchange($event, tax.paidamount, tax.refundamount)" /> 

and controller:

onchange($event, maxvalue, refundamount) {     return $event > maxvalue ? maxvalue : refundamount; } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -