Wednesday, 29 April 2015

angularjs number = number+1 will return me a very big number

In my angular app i have two buttons to increase and decrease a number.




My ng-click code for buttons are:

$scope.decreaseTableFontSize = function(){
    $scope.tableFontSize = $scope.tableFontSize - 1;
}

$scope.increaseTableFontSize = function(){
    $scope.tableFontSize = $scope.tableFontSize + 1;
}

However, sometime I can get a very big number after I clicked the "+" button.

I finally realise that sometime angular trade my variable --- $scope.tableFontSize as a string. So for example if the current value is 15 it will return me 151, because "15" + 1 =  151.

My solution is:

$scope.tableFontSize = Number($scope.tableFontSize) + 1;

No comments:

Post a comment