总不更新 - Angular JS范围

时间:2017-11-23 15:36:49

标签: angularjs angularjs-ng-repeat

<tbody ng-init="cartArray.total = {}">
    <tr ng-repeat="item in cartArray">
        <td class="cart_product"><a href="#"><img ng-src="images/products/{{item.productImageName}}" alt="Product"></a></td>
        <td class="cart_description">
            <p class="product-name"><a href="#">{{item.prodName}} </a></p>
            <small><a href="#">Color : Red</a></small>
            <small><a href="#">Size : M</a></small>
        </td>
        <td class="availability in-stock"><span class="label">In stock</span></td>
        <td class="price"><span>Rs. {{item.prodAmt}}</span></td>
        <td class="qty"><input ng-model="qty" class="form-control input-sm" type="text"></td>
        <td class="price"><span ng-init='cartArray.total.prodtotAmt = (+cartArray.total.prodAmt*qty) + (+item.prodAmt*qty)'>{{item.prodAmt*qty}}</span></td>
        <td class="action"><a ng-click="removeFromCart(item)"><i class="icon-close"></i></a></td>
    </tr>
</tbody>
<tfoot>
    <tr>
        <td colspan="2" rowspan="2"></td>
        <td colspan="3">Total products (tax incl.)</td>
        <td colspan="2">Rs {{cartArray.total.prodtotAmt}}</td>
    </tr>
</tfoot>

使用上面的代码,当我更改qty文本框的值时,相关产品的总金额会发生变化,但是当cartArray.total.prodtotAmt导致值不正确时,有人会对此有所帮助吗?

1 个答案:

答案 0 :(得分:0)

根据我的理解,您正试图获得总购车数量

<强> HTML

<tr ng-repeat="item in cartArray">
    <td class="cart_product"><a href="#"><img ng-src="images/products/{{item.productImageName}}" alt="Product"></a></td>
    <td class="cart_description">
        <p class="product-name"><a href="#">{{item.prodName}} </a></p>
        <small><a href="#">Color : Red</a></small>
        <small><a href="#">Size : M</a></small>
    </td>
    <td class="availability in-stock"><span class="label">In stock</span></td>
    <td class="price"><span>Rs. {{item.prodAmt}}</span></td>
    <td class="qty">
        <input ng-model="item.qty" class="form-control input-sm" type="number">
    </td>
    <td class="price"><span>{{item.prodAmt*qty}}</span></td>
    <td class="action"><a ng-click="removeFromCart(item)"><i class="icon-close"></i></a></td>
</tr>

页脚

<tr>
    <td colspan="2" rowspan="2"></td>
    <td colspan="3">Total products (tax incl.)</td>
    <td colspan="2">Rs <span ng-bind="ctrl.getTotalCartAmount()"></span></td>
</tr>

<强> JS

ctrl.getTotalCartAmount = function(){
    var total = 0;

    for(var i = 0; i<$scope.cartArray.length; i++){
       var item = $scope.cartArray[i];
       total += item.prodAmt*item.qty;
    }

    return total;
}