Search code examples
angularjsng-controller

how to print a value from function in angularjs


Have a look at the code in images below:

HTML:

enter image description here

JS:

enter image description here

cartAmt = productDetail.discountedPrice++; this is not working

${{cart.cartAmt}} this value is not shown up


Solution

  • In your code cartAmt is a global variable, add it to scope.

    // Set cart amount to zero initially
    $scope.cartAmt = 0    
    
    
    $scope.cart= function(){   
        $scope.cartAmt += $scope.event.productDetail.discountedPrice; 
    }
    

    And in your HTML, you can access it like this:

    <ins>cartAmt</ins>
    

    Also as I mentioned in comments, following line has a logical error.

    $scope.cartAmt = $scope.event.productDetail.discountedPrice++; 
    

    You should use:

    $scope.cartAmt += +$scope.event.productDetail.discountedPrice;