Angular 1.4.x 带有 gettersetter 的数字字段不适用于十进制数字
Angular 1.4.x numeric field with gettersetter not working for decimal numbers
我在 chrome(47) 中遇到一个问题,当结合使用数字类型的输入和 ng-model-options="{ getterSetter: true }" 时,不允许您输入小数在外地。
使用 getterSetter:(不工作)
<input ng-model="amount" ng-model-options="{ getterSetter: true }" step="0.01" type="number" >
没有 getterSetter:(有效)
<input ng-model="_amount" step="0.01" type="number">
查看 plunker 的演示
http://plnkr.co/edit/qu8UXCUtkJaFwjgGE1NX?p=preview
发生的事情是,当您声明一个 getterSetter
函数时,每次您修改输入的值时都会调用该函数。
所以当你写“12”时。该函数正在被调用,但它不是一个有效的数字,所以它去掉了“。”用于提供有效值。
尝试输入“123”,然后添加一个“.”在有效的数字之间,例如“12.3”!
编辑
我已经修复了您的代码,现在可以使用了。
试试这个:
$scope.amount = function(newValue) {
return arguments.length ? ($scope._amount = newValue) : $scope._amount;
};
这是一个分叉的 plunkr:http://plnkr.co/edit/xZtZLH5He4ZnjkqhFApY?p=preview
我在 chrome(47) 中遇到一个问题,当结合使用数字类型的输入和 ng-model-options="{ getterSetter: true }" 时,不允许您输入小数在外地。
使用 getterSetter:(不工作)
<input ng-model="amount" ng-model-options="{ getterSetter: true }" step="0.01" type="number" >
没有 getterSetter:(有效)
<input ng-model="_amount" step="0.01" type="number">
查看 plunker 的演示 http://plnkr.co/edit/qu8UXCUtkJaFwjgGE1NX?p=preview
发生的事情是,当您声明一个 getterSetter
函数时,每次您修改输入的值时都会调用该函数。
所以当你写“12”时。该函数正在被调用,但它不是一个有效的数字,所以它去掉了“。”用于提供有效值。
尝试输入“123”,然后添加一个“.”在有效的数字之间,例如“12.3”!
编辑
我已经修复了您的代码,现在可以使用了。
试试这个:
$scope.amount = function(newValue) {
return arguments.length ? ($scope._amount = newValue) : $scope._amount;
};
这是一个分叉的 plunkr:http://plnkr.co/edit/xZtZLH5He4ZnjkqhFApY?p=preview