I have an input field on my html that I need to limit the value of.
I am trying doing like this:
HTML:
<div *ngFor="let option of options">
<input type="text" [ngModel]="option.value" (change)="onChangeValue($event.target.value, option)">
</div>
Typescript:
onChangeValue(valueString: string, option: OptionViewModel) {
var value: number = parseInt(valueString);
// Check some conditions. Simplified code just for example
if (option.value != value && value > 5) {
value = 5;
}
option.value = value;
}
OptionViewModel:
export class OptionViewModel {
public optionId: number;
public description: string;
public pollId: number;
public value?: number;
}
I tried using two-way binding, but my code relies on the previous value of option.value, and using two-way binding changes the variable before entering the function.
The problem is, sometimes the input field is not being updated. It looks like I just work the first time the value need to be changed, so for example, if input 6 the field is correctly changed to 5, but then if I add a 0 (making it 50), it doesn't correct to 5.
I debbuged the code and it is running the function and changing the option.value, and even using Augury to inspect the objects show the correct value.
OptionViewModelcontain several fields? Can you show us that class (if not too large)?