私は6行4列のテーブルを持っています(4つ目は合計:これは上記の3つの列の合計になります)。基本的には、各列の合計を計算し、4列目の合計数を表示します。私のテーブルはng-repeatを使って生成されます。Angular.Js他の3つの入力の合計で入力を動的に更新する
のような何か:NGリピートから
<table>
<thead>
<tr>
<th ng-repeat="item in items">{{item}}</th>
</tr>
</thead>
<tbody>
<tr>
<td>Input1:</td>
<td ng-repeat="collection in collections">
<input type="text" ng-model="collection.row1">
</td>
</tr>
<tr>
<td>Input2:</td>
<td ng-repeat="collection in collections">
<input type="text" ng-model="collection.row2">
</td>
</tr>
<tr>
<td>Input3:</td>
<td ng-repeat="collection in collections">
<input type="text" ng-model="collection.row3">
</td>
</tr>
<tr>
<td>Sum:</td>
<td ng-repeat="collection in collections" ng-model="collection.total">
{{collection.total}}
</td>
</tr>
</tbody>
</table>
「コレクションは、」私はGETメソッドを使用してAPIから取得し、$スコープに自分のデータを格納しています6つのオブジェクトの配列になります。
ng-modelの「Total」行は、計算が完了してバックエンドから来ていますが、動的に更新されているので、クライアントに表示する方法が必要です。
私は$ watchと$ watchCollectionを試しましたが、また変更することができましたが、動作させる方法が見つかりません。私のコントローラーでは、オブジェクトの配列を調べるためにforを使用し、すべての位置を合計しようとしましたが、それは機能しませんでした。
私の問題の別の解決方法はありますか?ここで
は、私は私のコントローラにしようとしたものです:
$scope.collections = [];
$scope.getTotal = function(){
var total = 0;
for(var i = 0; i < $scope.collections[i].length; i++){
var myItems= $scope.collections[i];
total = (myItems.row1+ myItems.row2+ myItems.row3);
}
return total;
};
ありがとうございました。
与え、あなたのコード内の
<td></td>
内の表示値のために競合しているあなたがしようとしているものを私たちに表示します。 .. – Rayonフィドルplzを提供してください:) –