Search code examples
htmlangularjsscopeoperand

use greater than operand in angularjs controller


Data in my controller

$scope.data = [{
                "F": "1,26,000"
            }];

$scope.data2 = [{
                "F": "26,000"
            }];

this is my data in my controller now i want to use if else condition here

if (Number($scope.data[0].F) > Number($scope.data2[0].F))
   {
      console.log('aaa');
   }
else {
      console.log('bbb');
     }

now if data is greater then data2 ('aaa') should print and of it is less then ('bbb') should print

Now as you see data is greater then data2

('aaa') this should be printed on my console but instead of that ('bbb') this is printing in console,

what i need to change in my controller??


Solution

  • Remove the commas so it's "126000" and "26000"

    Can also do

    if (parseInt($scope.data[0].F) > parseInt($scope.data2[0].F))
       {
          console.log('aaa');
       }
    else {
          console.log('bbb');
         }
    

    parseInt converts the string to a number.