I've got a component where I'm trying to enforce maxlength by truncating anything the user types over its maxlength.
Here's how I'm doing that:
if (element.maxLength) {
if (element.value.length >= element.maxLength) {
this.parts[part] = parseInt(element.value.substr(0,element.maxLength));
}
}
this.parts
is a collection of three inputs. part
is sent in through the update function and is used to refer to the specific input.
Whenever the maxlength is reached (or exceeded), I set the value on the model to a substring from 0 to maxlength. This is setting the correct value of this.parts[part]
, but it's not showing up in the view. What'm I doing wrong?
Here's my template:
<input type="number" id="ssn3" [(ngModel)]="parts.a" maxlength="3" autotab="ssn2" (input)="update('a',$event.target)" />-
<input type="number" id="ssn2" [(ngModel)]="parts.b" maxlength="2" autotab="ssn4" (input)="update('b',$event.target)" />-
<input type="number" id="ssn4" [(ngModel)]="parts.c" maxlength="4" (input)="update('c',$event.target)" />
Autotab is a custom directive I wrote that can tab to the next input whenever maxlength is reached. This prevents the first two inputs from having values longer than their maxlength, but not the last one, of course