Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用angularjs创建动态发票_Angularjs - Fatal编程技术网

使用angularjs创建动态发票

使用angularjs创建动态发票,angularjs,Angularjs,我尝试创建一个动态发票,我有数量和价格列,然后我得到产品的总价值,所以当我更改数量值时,价格和总价值应该更改,我找到一种方法更改价格,但总价值不起作用: <div class="container" ng-controller="OrderController" ng-init="quantity=1"> <div id="order-detail-content" class=" row"> <table id="cart_summary">

我尝试创建一个动态发票,我有数量和价格列,然后我得到产品的总价值,所以当我更改数量值时,价格和总价值应该更改,我找到一种方法更改价格,但总价值不起作用:

<div class="container" ng-controller="OrderController" ng-init="quantity=1">
<div id="order-detail-content" class=" row">
    <table id="cart_summary">
        <thead>
            <tr>
                <th>Total</th>
                <th>&nbsp;</th>
                <th>Qty</th>
                <th>Unit price</th>
                <th>Availability</th>
                <th>Description</th>
                <th>Product</th>

            </tr>
        </thead>
        <tfoot>

            <tr class="cart_total_price">
                <td colspan="2" class="price" id="total_product">{{totalValueOfProducts}}</td>
                <td colspan="3" class="text-right">Total</td>
                <td rowspan="4" colspan="3" id="cart_voucher" class="cart_voucher"></td>
            </tr>

        </tfoot>
        <tbody>

            <tr ng-repeat="order in Orders track by $index" >
                <td class="cart_total" data-title="Total">
                    <span class="price" id="total_product_price_3_13_0">
                        {{order.Price * quantity}}
                    </span>
                </td>

                <td >
                    <input size="2" ng-model="quantity" type="text" value="1" name="quantity_3_13_0_0">
                </td>
                <td>
                    <ul class="price text-right" id="product_price_3_13_0">
                        <li ng-model="order.Price" class="price"> {{order.Price}}</li>
                    </ul>
                </td>
                <td class="cart_description">
                    <p style="color:black" class="product-name">{{order.ProductName}}</p>
                    <hr />
                    <small style="color:black" class="cart_ref">{{order.ProductDescription}}</small>
                </td>
                <td class="cart_product">
                    <img ng-src="" alt="Printed Dress" width="98" height="98">
                </td>
            </tr>

        </tbody>
    </table>
</div> <!-- end order-detail-content -->

当我更改
数量
的值时,
产品的总价值
的值没有更改!为什么
$watch
不起作用?有什么问题吗?

'quantity'需要在$scope中才能进行绑定


另一方面,最好将所有数据(订单、数量等)移动到一个服务中,最好遵循angular的MVC范式

你的意思是什么?你是说我把数量放在:$scope.quantity=1而不是ng init中?
<script>
app.controller('OrderController', function ($scope, $http) {
    $scope.Orders = {};
    $scope.GetOrders = function () {
        $http.get('/Order/GetAllOrders').success(function (response) {
            $scope.Orders = response;
            //debugger;
            GetTotalValueOfProducts(response);
        });
    }
    $scope.GetOrders();

    GetTotalValueOfProducts = function (response) {
        //debugger;
        $scope.totalValueOfProducts = 0;
        for (var i = 0; i < response.length; i++) {

            $scope.totalValueOfProducts += response[i].Price * $scope.quantity;
        }
    }

    $scope.$watch('quantity', function () {
        debugger;
        $scope.GetOrders();
    });
});
</script>