HTML/JS 不解析浮点数
HTML/JS not parsing floats
我有一个平均值,只要用户更改 his/her 输入,我就需要更新到新的平均值。不幸的是,HTML 似乎只有在输入 int 而不是 float 时才会启动更改。更具体:
<input type="number" step="0.01" ng-model="newRating" max="10" min="0" maxlength="2">
{{averageRating}}
AngularJS:
Parent Controller:
$scope.sumOfRatings is the previous sum of ratings,
newRating is the rating inputed,
$scope.volumeOfRatings is the previous number of rating:
$scope.$on('changeRating', function(eventName, newRating) {
$scope.averageRating = ($scope.sumOfRatings + newRating) / ($scope.volumeOfRatings + 1);
}
Code in Child Controller (parses the user input):
$scope.$watch('newRating', function(newRating, oldRating) {
$scope.$emit('changeRating', $scope.newRating);
});
但是,这仅在解析 3、10、8、5 等数字时有效。如果我输入 0.33 或 9.8 之类的值,这对平均值没有任何影响。对不起,如果我输错了任何代码,但我认为主要问题不是 AngularJS 代码,而是 HTML 或 AngularJS 识别更改。我在 $watch 中做了一个 console.log(newRating) ,发现它从来没有解析过任何十进制输入(比如 8.55),只输出了 8.
如何解析 HTML 中的十进制变化?我尝试使用 ng-repeat 指令无济于事。
删除输入标签的最大长度。
<input type="number" step="0.01" ng-model="newRating" max="10" min="0">
{{averageRating}}
我有一个平均值,只要用户更改 his/her 输入,我就需要更新到新的平均值。不幸的是,HTML 似乎只有在输入 int 而不是 float 时才会启动更改。更具体:
<input type="number" step="0.01" ng-model="newRating" max="10" min="0" maxlength="2">
{{averageRating}}
AngularJS:
Parent Controller:
$scope.sumOfRatings is the previous sum of ratings,
newRating is the rating inputed,
$scope.volumeOfRatings is the previous number of rating:
$scope.$on('changeRating', function(eventName, newRating) {
$scope.averageRating = ($scope.sumOfRatings + newRating) / ($scope.volumeOfRatings + 1);
}
Code in Child Controller (parses the user input):
$scope.$watch('newRating', function(newRating, oldRating) {
$scope.$emit('changeRating', $scope.newRating);
});
但是,这仅在解析 3、10、8、5 等数字时有效。如果我输入 0.33 或 9.8 之类的值,这对平均值没有任何影响。对不起,如果我输错了任何代码,但我认为主要问题不是 AngularJS 代码,而是 HTML 或 AngularJS 识别更改。我在 $watch 中做了一个 console.log(newRating) ,发现它从来没有解析过任何十进制输入(比如 8.55),只输出了 8.
如何解析 HTML 中的十进制变化?我尝试使用 ng-repeat 指令无济于事。
删除输入标签的最大长度。
<input type="number" step="0.01" ng-model="newRating" max="10" min="0">
{{averageRating}}