Angular2 - 输入字段的两种方式绑定
Angular2 - Two way binding for input Field
我有一个数字类型的输入字段。当用户输入多个零 (0) 并移至下一个输入字段时,多个零应返回到单个 0。
我在 plunkr 中尝试了以下代码:https://plnkr.co/edit/dyCp5ZMKOkZvcsw4F8og?p=preview
<input [(ngModel)]="value" type="number" class="form-control" id="valueId"
(ngModelChange)="valuechange($event)">
valuechange(newValue) {
//newValue = newValue.toString().replace(/^0+/, '0');
newValue=parseInt(newValue.toString());
console.log(newValue);
}
只需要在值为0时将0
设为字符串,调用onchange函数即可。
像这样
<input [(ngModel)]="value" type="number" class="form-control" id="valueId"
(change)="valuechange($event)">
if(this.value === 0){
this.value = '0';
}
PS:不需要使用parseInt
和toString()
进行转换
我有一个数字类型的输入字段。当用户输入多个零 (0) 并移至下一个输入字段时,多个零应返回到单个 0。
我在 plunkr 中尝试了以下代码:https://plnkr.co/edit/dyCp5ZMKOkZvcsw4F8og?p=preview
<input [(ngModel)]="value" type="number" class="form-control" id="valueId"
(ngModelChange)="valuechange($event)">
valuechange(newValue) {
//newValue = newValue.toString().replace(/^0+/, '0');
newValue=parseInt(newValue.toString());
console.log(newValue);
}
只需要在值为0时将0
设为字符串,调用onchange函数即可。
像这样
<input [(ngModel)]="value" type="number" class="form-control" id="valueId"
(change)="valuechange($event)">
if(this.value === 0){
this.value = '0';
}
PS:不需要使用parseInt
和toString()