html:
<mat-form-field appearance="outline"class="inputBox">
<input matInput name="total" placeholder="0.00" [ngModel]="total"
(ngModelChange)="handleOnChange('total', $event)" />
</mat-form-field>
TS:
public handleOnChange = (field, e) => {
console.log("hell", field)
this.total=90
}
the value of this.total should remain constant at 90 after any change event triggers the handleOnChange method and 90 should be appear as value in the input box constantly.This is the logic I am trying to achieve.. but when there is a onChange at first time, value of input is changed to 90 but after first time when any key is pressed,value is changed..eg..90a,90tyhuil,90oojh….
i come from react..anyone please suggest me how to achieve the onChange event in input tag of react in angular.
2
Answers
Remove (ngModelChange) event
Now Initialise total’s value in constructor so it will remain same until input change
Now replace [ngModel]="total" with [(ngModel)]="total" and it will automatically assign new value into total and it will reflect in input and add [value]="total" for reflating initial value into input
So final code will be
We can just pass the input field directly to function and set the value to the desired result, this will override any changes!
html
ts
Stackblitz Demo