角度得分控制器附加的值不能正确递增

时间:2017-02-08 07:46:38

标签: angularjs

有人可以看看这个控制器

分数没有将得分cookie增加1,例如1,2,3,根据需要,但只加1,所以不希望的结果是11 111 1111

$scope.score = localStorage.getItem("token");
                $scope.noOfTries = 0;

                $scope.original = $scope.pageTitle;
                $scope.guess = null;
                $scope.deviation = null;

                $scope.verifyGuess = function () {
                    $scope.deviation = $scope.original - $scope.guess;

                    $scope.noOfTries = $scope.noOfTries + 1;
                    $scope.falseAnswer = $scope.deviation != 0;
                    if ($scope.deviation == 0 && $scope.noOfTries == 1){
                        $scope.score = $scope.score += 1;
                        var token = $scope.score;
                        localStorage.setItem("token", //...)
                    };
               }
}

3 个答案:

答案 0 :(得分:2)

这应该有帮助;

$scope.score = localStorage.getItem("token")) ? parseInt(localStorage.getItem("token")) : 0;
                $scope.noOfTries = 0;

                $scope.original = $scope.pageTitle;
                $scope.guess = null;
                $scope.deviation = null;

                $scope.verifyGuess = function () {
                    $scope.deviation = $scope.original - $scope.guess;

                    $scope.noOfTries = $scope.noOfTries + 1;
                    $scope.falseAnswer = $scope.deviation != 0;
                    if ($scope.deviation == 0 && $scope.noOfTries == 1){
                        $scope.score = $scope.score += 1;
                        var token = $scope.score;
                        localStorage.setItem("token", token)
                    };
               }
}

答案 1 :(得分:2)

将您的$scope.score = $scope.score += 1;更改为:

$scope.verifyGuess = function () {
    $scope.deviation = $scope.original - $scope.guess;

    $scope.noOfTries = $scope.noOfTries + 1;
    $scope.falseAnswer = $scope.deviation != 0;
    if ($scope.deviation == 0 && $scope.noOfTries == 1){
        $scope.score = parseInt($scope.score) + 1;
        var token = $scope.score;
        localStorage.setItem("token", //...)
    };
}

答案 2 :(得分:1)

只需了解operators在JavaScript中的工作原理。

因此,简而言之,当您使用Sting进行'+'运算符时,它会进行连接,当您使用数字执行'+'运算符时,它会执行加法。

在你的情况下,'$scope.score'似乎是字符串。所以它正在执行连接。将它编号,它将作为添加运算符。使用parseInt进行转化。

$ scope.score = parseInt(localStorage.getItem(" token"),10);

所以你的代码看起来像

$scope.score = parseInt(localStorage.getItem("token"),10);
                $scope.noOfTries = 0;

                $scope.original = $scope.pageTitle;
                $scope.guess = null;
                $scope.deviation = null;

                $scope.verifyGuess = function () {
                    $scope.deviation = $scope.original - $scope.guess;

                    $scope.noOfTries = $scope.noOfTries + 1;
                    $scope.falseAnswer = $scope.deviation != 0;
                    if ($scope.deviation == 0 && $scope.noOfTries == 1){
                        $scope.score = $scope.score + 1;
                        var token = $scope.score;
                        localStorage.setItem("token", //...)
                    };
               }
}

编辑

为什么推荐Radix传递

  

如果radix未定义或0(或不存在),则JavaScript假设为   以下内容:

     
      
  • 如果输入字符串以" 0x"开头?或" 0X",基数为16(十六进制),并解析字符串的其余部分。

  •   
  • 如果输入字符串以" 0"开头,则基数为八(八进制)或十进制(十进制)。确切选择哪个基数是
      实现有关。 ECMAScript 5指定10(十进制)
      使用,但并非所有浏览器都支持此功能。出于这个原因
      使用parseInt时总是指定一个基数。

  •   
  • 如果输入字符串以任何其他值开头,则基数为10(十进制)

  •   

有关详细信息,请关注mozilla