Click here to Skip to main content
14,689,404 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have to use custom scope varibale within ng-repeat using angularjs, but it will return last value to all

What I have tried:

<div ng-repeat="pro in UserProduct">
{{discountvalue}}
</div>

<script>
 $http.get('/api/Product/ListOnlyProductOrderFeatured/', { params: {} }).success(function (data) {
$scope.UserProduct = data;
for (var i = 0; i < $scope.UserProduct.length; i++) {
$scope.generalprice = $scope.UserProduct[i].ec_shop_product_price;
$scope.discountprice = $scope.UserProduct[i].ec_shop_product_discount;

$scope.discountvalue = $scope.generalprice - $scope.discountprice;

}
})
</script>
Posted
Updated 16-Mar-18 19:45pm

Probably because you're overwriting the same variable on each iteration of the loop.

Try storing the value on each item instead:
for (var i = 0; i < $scope.UserProduct.length; i++) {
    var product = $scope.UserProduct[i];
    var generalPrice = product.ec_shop_product_price;
    var discountPrice = product.ec_shop_product_discount;
    product.discountvalue = generalPrice - discountPrice;
}
   
Thanks for your reply its working fine....
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900