Skip to content Skip to sidebar Skip to footer

Arithmetic Addition (+) Is Not Working In Ng-bind

There is an object having objData.pendingCount = '2',objData.refundCount = '1',objData.saleCount = 43; I performed on them and angular will take care of the rest!

<input ng-model="objData.pendingCount"type="number" />
...

The alternative is to simply prefix + to force a number conversion, like so

<spanng-bind="((+objData.pendingCount + +objData.refundCount)/ objData.saleCount * 100).toFixed(2)"></span>

Why you CANNOT use Number(...) to make a string a number in an Angular expression

Using Number to convert the string to a number will not work directly because (from https://docs.angularjs.org/guide/expression)

Context: JavaScript expressions are evaluated against the global window. In Angular, expressions are evaluated against a scope object.

So if you need to use Number you can do something like

$scope.Number = $window.Number;

in your controller before you use it in your expression. The same goes for other global window properties.

Solution 2:

corrects your expression:

<tdng-bind="(objData.pendingCount +objData.refundCount)/ (objData.saleCount * 100).toFixed(2)"></td>

is missing an open parentheses

Solution 3:

I solved it using

<tdng-bind="(sum(objData.pendingCount,objData.refundCount)/objData.saleCount*100).toFixed(2)"></td>

and in the controller

$scope.sum = function (a, b) {
   returnparseInt(a) + parseInt(b);
}

and it is working fine.

Post a Comment for "Arithmetic Addition (+) Is Not Working In Ng-bind"