Angular2 forms Angular2-输入字段的双向绑定

Angular2 forms Angular2-输入字段的双向绑定,angular2-forms,two-way-binding,Angular2 Forms,Two Way Binding,我有一个数字类型的输入字段。当用户输入多个零(0)并移动到下一个输入字段时,多个零应返回到单个0 我在plunkr中尝试了以下代码: 值更改(新值){ //newValue=newValue.toString().replace(/^0+/,'0'); newValue=parseInt(newValue.toString()); console.log(newValue); } 当值为0时,只需将0设置为字符串,并调用函数onchange即可。 像这样 <in

我有一个数字类型的输入字段。当用户输入多个零(0)并移动到下一个输入字段时,多个零应返回到单个0

我在plunkr中尝试了以下代码:


值更改(新值){
//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';
}

如果(this.value==0){
this.value='0';
}
PS:无需使用
parseInt
toString()进行转换


您的plunker有不同的代码,请检查并更新plunker谢谢。你救了我一天@pradeep JainGlad想知道@shweta,如果你得到了你想要的,请将其标记为已接受并投票!!这个有效的例子是一个巨大的帮助
<input [(ngModel)]="value" type="number" class="form-control" id="valueId" 
      (change)="valuechange($event)">


if(this.value === 0){
      this.value = '0';
}