Javascript 计算总外ng重复次数

Javascript 计算总外ng重复次数,javascript,angularjs,Javascript,Angularjs,我正在尝试计算总输出ng repeat,它取决于ng change内部ng repeat 我的桌子看起来像这样 <table> <tr> <td>Total - total is {{tot}}</td> </tr> <tr ng-repeat="obj in Array"> <td> <input ng-change="g

我正在尝试计算总输出
ng repeat
,它取决于
ng change
内部
ng repeat

我的桌子看起来像这样

<table>
    <tr>
        <td>Total - total is {{tot}}</td>
    </tr>

    <tr ng-repeat="obj in Array">
         <td>
            <input ng-change="getValue(obj.Cost);" ng-model="obj.Quantity" type="number">
         </td>
        <td>
            <span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
        </td>
        <td> 
            <input type="number" ng-value="obj.Cost = obj.Quantity * obj.ListPrice">
        </td>
    </tr>
</table>
<span>Total is - {{total}}</span>    
<table>
    <tbody ng-init="total = 0">
    <tr ng-repeat="obj in Array">
         <td>
            <input ng-change="getValue(obj.Cost);" ng-model="obj.Quantity" type="number">
         </td>
        <td>
            <span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
        </td>
        <td ng-init="$parent.total = $parent.total + (obj.Quantity * obj.ListPrice)"> 
            <input type="number" ng-value="obj.Cost = obj.Quantity * obj.ListPrice">
        </td>
    </tr>
</table>

但事实并非如此。正确的方法是什么?

您的代码中有一些问题,我可以从中看出

首先

<span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
您正在将值赋值给
obj.Cost
ng value
的操作。我只希望你能理解这一点,我不确定是否有更好的方式来描述这一点

我相信你想要达到的是

<input type="number" 
       ng-init="obj.Cost = obj.Quantity * obj.ListPrice" 
       ng-value="obj.Cost" />

您的代码中有一些问题,我可以马上看到

首先

<span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
您正在将值赋值给
obj.Cost
ng value
的操作。我只希望你能理解这一点,我不确定是否有更好的方式来描述这一点

我相信你想要达到的是

<input type="number" 
       ng-init="obj.Cost = obj.Quantity * obj.ListPrice" 
       ng-value="obj.Cost" />

您的代码不起作用,因为在它可以计算
目标成本之前,
ng change
被触发,并且您得到了
成本的陈旧/旧值。因此,你可以这样做:

$scope.getValue = function() {
  $scope.total = 0
  $scope.myArray.forEach(function(arr) {
    $scope.total += arr.Quantity * arr.ListPrice
  })
}
您的
输入将是:

<td>
  <input ng-change="getValue()" ng-model="obj.Quantity" type="number">
</td>

总计-总计为{Total}
{{obj.ListPrice}

您的代码不起作用,因为在它可以计算
目标成本之前,
ng change
被触发,并且您得到了
成本的陈旧/旧值。因此,你可以这样做:

$scope.getValue = function() {
  $scope.total = 0
  $scope.myArray.forEach(function(arr) {
    $scope.total += arr.Quantity * arr.ListPrice
  })
}
您的
输入将是:

<td>
  <input ng-change="getValue()" ng-model="obj.Quantity" type="number">
</td>

总计-总计为{Total}
{{obj.ListPrice}

您可以像这样访问总外部
ng repeat

<table>
    <tr>
        <td>Total - total is {{tot}}</td>
    </tr>

    <tr ng-repeat="obj in Array">
         <td>
            <input ng-change="getValue(obj.Cost);" ng-model="obj.Quantity" type="number">
         </td>
        <td>
            <span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
        </td>
        <td> 
            <input type="number" ng-value="obj.Cost = obj.Quantity * obj.ListPrice">
        </td>
    </tr>
</table>
<span>Total is - {{total}}</span>    
<table>
    <tbody ng-init="total = 0">
    <tr ng-repeat="obj in Array">
         <td>
            <input ng-change="getValue(obj.Cost);" ng-model="obj.Quantity" type="number">
         </td>
        <td>
            <span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
        </td>
        <td ng-init="$parent.total = $parent.total + (obj.Quantity * obj.ListPrice)"> 
            <input type="number" ng-value="obj.Cost = obj.Quantity * obj.ListPrice">
        </td>
    </tr>
</table>
Total是-{{Total}
{{obj.ListPrice}

基本上,您在
ng repeat
内部执行sum,在
tbody
内部执行
ng init
,这样您就可以访问
total
外部
ng repeat

您可以像这样访问
ng repeat
外部的total

<table>
    <tr>
        <td>Total - total is {{tot}}</td>
    </tr>

    <tr ng-repeat="obj in Array">
         <td>
            <input ng-change="getValue(obj.Cost);" ng-model="obj.Quantity" type="number">
         </td>
        <td>
            <span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
        </td>
        <td> 
            <input type="number" ng-value="obj.Cost = obj.Quantity * obj.ListPrice">
        </td>
    </tr>
</table>
<span>Total is - {{total}}</span>    
<table>
    <tbody ng-init="total = 0">
    <tr ng-repeat="obj in Array">
         <td>
            <input ng-change="getValue(obj.Cost);" ng-model="obj.Quantity" type="number">
         </td>
        <td>
            <span ng-model="obj.ListPrice">{{obj.ListPrice}}</span>
        </td>
        <td ng-init="$parent.total = $parent.total + (obj.Quantity * obj.ListPrice)"> 
            <input type="number" ng-value="obj.Cost = obj.Quantity * obj.ListPrice">
        </td>
    </tr>
</table>
Total是-{{Total}
{{obj.ListPrice}

基本上,您在
ng repeat
内部进行sum,在
tbody
内部进行
ng init
,这样您就可以访问
total
外部
ng repeat

先生,我认为您需要使用angular.forEach(数组、函数(项目、索引){total+=parseInt(项目成本);})$范围:tot=总计;getValue函数中的这行代码。还有一件事我想你需要一些修改,不需要调用ng change=“getValue(obj.Cost);”我有一个解决方案,如果你有任何问题,请敲一下mesir,我想你需要使用angular.forEach(数组,函数(项,索引){total+=parseInt(项.成本);})$范围:tot=总计;getValue函数中的这行代码。还有一件事,我想你需要一些改变,不需要打电话给ng change=“getValue(obj.Cost);“我有一个解决方案,如果你有任何问题,请尽快联系我。”。。我在做$scope.total+=arr.cost,这是稍后创建的。干杯谢谢你抽出时间。。我在做$scope.total+=arr.cost,这是稍后创建的。干杯