user7067778
user7067778

Reputation:

passing the data in $scope variable

I am getting the data from my web service and storing the same data into my scope variable like

then(function (Tlist) {
                    $scope.Tlist=Tlist.data;
                })

and displaying the same data into table, now in my table i am selecting the row by checkbox like

                    <td>
                        <input type="checkbox" ng-checked="checkall" ng-model="Ldata.checked" data-ng-click="calculateTotal(Ldata)" />
                    </td>

and in my "calculateTotal(Ldata)" function i want to store the value of (Ldata.amount) into another $scope variable

this is how my condition inside the function looks

        if (Ldata.checked) {
            $scope.total += Number(Ldata.amount);
            console.log(Ldata.amount);
            console.log(Number($scope.total));
        }

but on

console.log(Number($scope.total));

line my result is coming as NaN but on this line and on console.log(Ldata.amount); this line my result is coming as 1400 so i am not able to understand why i am unable to pass data from one variable to another

Upvotes: 0

Views: 67

Answers (2)

Krzysiek
Krzysiek

Reputation: 98

You receive NaN probably because the $scope.total is undefined. If you really want to use += operator you need to know two things:

  • a += b is a shortcut of a = a + b
  • any math operation on number value and undefined will return NaN value

Just set the initial value to $scope.total - for example at the beginning of your controller definition set $scope.total = 0;.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

Instead of Number use parseInt

 console.log(parseInt($scope.total));

Upvotes: 0

Related Questions