I have a shop component and I need to disallow people to pick negative numbers in my input.
For example, if user will type -3 in my input, the input value will immadiately change to 0.
How I can control my angular 2 input OnChange value to set it back to 0 it the value is negative?
Tried to use (ngModelChange)="myMethod($event)"
and set the parameter to 0 if it's negative, but didn't work.
min
and step
to prevent the spinner from going negative. (reference: this SO answer)ngModelChange
to handle the case where a user tries to type in a negative number.@Component({
selector: 'my-app',
template: `{{title}}
<p><input type="number" min="0" step="1" [ngModel]="model"
(ngModelChange)="validate($event)">`,
})
export class AppComponent {
title = `Angular - RC.1`;
model = 3;
validate(value) {
value < 0 ? this.model = 0 : this.model = value;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments