1

I have a issue with AngularJS and very basic math. When I have picked the data from my firebase, I want to addition them.

Here's my code:

  $scope.project.$on('loaded', function(value) {
  console.log(value); // data loaded from Firebase
  console.log(value.name); // subset of the returned value

  angular.isNumber(value.firstmatch);
  angular.isNumber(value.secondmatch);



  $scope.project.points = ((value.firstmatch) + (value.firstmatch));

And this outputs 11. And yes, the firstmatch value is 1.

Thanks!

Pim Praat
  • 43
  • 1
  • 8
  • 4
    Adding in this case is being interpreted as string concatenation, not number addition: '1' + '1' = '11'. – duffymo Feb 07 '14 at 15:51
  • [This can help](http://stackoverflow.com/questions/17351831/why-is-angular-isnumber-not-working-as-expected), look at the comments – lavrik Feb 07 '14 at 15:53

1 Answers1

6

So your data is giving you strings, just convert them to numbers:

 $scope.project.$on('loaded', function(value) {
  console.log(value); // data loaded from Firebase
  console.log(value.name); // subset of the returned value

  //angular.isNumber(value.firstmatch);
  //angular.isNumber(value.secondmatch);



  $scope.project.points = parseInt(value.firstmatch, 10) + parseInt(value.firstmatch,10);
Laurent
  • 3,798
  • 25
  • 22
Thierry Blais
  • 2,848
  • 22
  • 41