Search code examples
angularjsstringintangularjs-ng-click

Angular: Convert parameter String inside ng-click


is it possible to convert String to int inside ng-click?

I have this data

$scope.cartList = [{ number: "1"}];

So in my view i just call number using ng-repeat( ng-repeat="cart in cartList"). I use number for increment by using ng-click.

<button data-ng-click="increment = 0 + cart.number></button>
<span>{{increment}}</span>

So here I got problem during calculation, I know cart.number is String but is there any way to convert cart.number to int inside ng-click?


Solution

  • Your logic is wrong . you are calculated with 0, it can't increase the count . So you should calculate with latest increment value instead of 0.

    Try this

    <button data-ng-click="increment = increment + parseInt(cart.number)"></button>
    <span>{{increment}}</span>
    

    Please assign parseInt in a scope object in controller. then use that in button

    $scope.parseInt = parseInt;
    

    You can check the demo here:

    angular.module("app",[])
    function Homecontroller($scope)
    {
    $scope.cartList = [{ number: "1"}];
    $scope.parseInt=parseInt;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="app" ng-controller="Homecontroller">
    <button data-ng-click="increment = increment + parseInt(cartList[0].number)">increase</button>
    <span>{{increment}}</span>
     </div>