I'm learning Angular with TypeScript, and a few inconsistencies with JavaScript are troubling me. Here's a function which works perfectly with pure JavaScript (it programmatically sets min and max attributes to an input - please don't ask me why I'd want to do that programmatically):
function change_number(input,min,max) {
if(input.value > max)
input.value = max;
else if(input.value < min)
input.value = min;
}
<input type='number' oninput='change_number(this,1,5)' placeholder='Enter a number' />
In Angular and TypeScript, the function behaves strangely: If I enter 10 into the input field, it doesn't reset to 5, until I enter another digit, meaning the value is only read after the input event (and not on-input - don't know if that makes sense). What's more strange is it works fine with the keydown and keyup events. Why this behaviour? Is it linked to the way Angular binds events to inputs? I have a feeling understanding this would help me better understand Angular's binding mechanism with NgModel
FYI, here's how I called the function in Angular (using the Ionic Framework) - I used the bound quantity in [(ngModel)] as the value of the input:
<ion-input type='number' (input)='change_numbers(1,5)' [(ngModel)]='quantity'></ion-input>