javascript 在 AngularJS 中解析整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16557689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:08:11  来源:igfitidea点击:

Parsing integers in AngularJS

javascriptmathangularjsstring-parsing

提问by Sriramajeyam Sugumaran

I am using AngularJSin one of our application.

我在我们的一个应用程序中使用AngularJS

In the code below, while loading the page the expression addition (number, valuetoadd) parses correctly and give output as 1+1=2but while changing the input it doesn't parse correctly and it gives output as `1+2=12'.

在下面的代码中,在加载页面时,表达式加法 (number, valuetoadd) 正确解析并给出输出,1+1=2但是在更改输入时,它没有正确解析并且给出输出为“1+2=12”。

Where do I need to change?

我需要在哪里改变?

<div ng-controller='MathsController'>
    <input type="range" min="0" max="100" step="1" value="1" ng-model='valuetoadd'/>

    <p>Addition table of <b>{{valuetoadd}}</b></p>

    <div ng-repeat='number in numbers'>
        {{number}} + {{valuetoadd}} = {{ addition(number,valuetoadd) }}
    </div>
</div>
<script type="text/javascript">
    function MathsController($scope) 
    {
        $scope.valuetoadd= 1;
        $scope.numbers =[1,2,3,4,5,6,7,8,9,10]; 
        $scope.addition = function(number,valuetoadd)
        {
            return number+valuetoadd;
        }
    }
</script>

回答by Tosh

May be numberbeging passed as a string. Try:

可能是number作为字符串传递的。尝试:

        $scope.addition = function(number, multiplier)
        {
            return + number + multiplier;
        }
        $scope.addition = function(number, multiplier)
        {
            return + number + multiplier;
        }

+would convert the string into number.

+将字符串转换为数字。

EDIT:Now, I see that multiplieris passed as string. So IT has to be converted instead...

编辑:现在,我看到它multiplier是作为字符串传递的。所以它必须被转换...

Sorry that I did not read the code carefully.

抱歉,我没有仔细阅读代码。

        $scope.addition = function(number, multiplier)
        {
            return number + +multiplier;
        }

Thank you @Artur for pointing this out.

谢谢@Artur 指出这一点。

回答by Artur Udod

Seems like the root of the problem lies here: https://github.com/angular/angular.js/issues/1189

似乎问题的根源在这里:https: //github.com/angular/angular.js/issues/1189

Also read this SO topic: Two-way binding with range and number input in AngularJS

另请阅读此 SO 主题:在 AngularJS 中使用范围和数字输入进行双向绑定

As a workaround you can use the solutions proposed by thefrontenderand tosh shimayama.

作为解决方法,您可以使用前端tosh shimayama提出的解决方案。

It's worth noticing, though, that it is the multiplierwho requires parsing to integer, not the number. See my comment to your question.

不过,值得注意的是,需要解析为整数的是乘数,而不是number. 请参阅我对您的问题的评论。

回答by Eugene Fidelin

Another quick fix is to multiply value by 1 before addition. This will force JavaScript to convert value from string to integer.

另一个快速解决方法是在加法之前将值乘以 1。这将强制 JavaScript 将值从字符串转换为整数。

Example:

例子:

{{value*1 + 10}}

{{值*1 + 10}}

回答by thefrontender

You need to coerce the variables into some sort of numeric type (values from HTML inputs are passed as string by default)

您需要将变量强制转换为某种数字类型(默认情况下,来自 HTML 输入的值作为字符串传递)

$scope.addition = function (number, multiplier) {
    return number + parseInt(multiplier, 10);
}