2017-02-04 4 views
1

から値を減算することによって列の値を設定します。はどのように</p> <p>は、私が入力したAMTの各欄に入力されたAMTのに基づいてバランスの列の値を設定したい場合は、以下snippsetはNGリピート値

私はこれを試してみましたが、私は1列のみここ

var app = angular.module("myApp", []); 
 
app.controller("myCtrl", function($scope) { 
 
    $scope.records = [ 
 
    { 
 
     "Amt" : "500", 
 
     
 
    }, 
 
    { 
 
     "Amt" : "800", 
 
     
 
    }, 
 
    { 
 
     "Amt" : "1580", 
 
    }, 
 
    { 
 
     "Amt" : "1122", 
 
    } 
 
    ] 
 
    
 
    $scope.value=function(d) 
 
    { 
 
    //How set the value of balace amout by subtrating Amt-Entered Amt. 
 
    //I tried this but in my scenarion, same vallue is setting for all balance column though i make change in only one column 
 
    } 
 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body ng-app="myApp"> 
 

 
<table ng-controller="myCtrl" border="1"> 
 
    <tr> 
 
    <td>Amt</td> 
 
    <td>Balance</td> 
 
    <td>Entered Amt</td> 
 
</tr> 
 
<tr ng-repeat="x in records"> 
 
    <td>{{x.Amt}}</td> 
 
    <td>{{balance}}</td> 
 
    <td><input type="text" ng-model="d" ng-change="value(d)"></td> 
 
</tr> 
 
</table> 
 

 
</body> 
 
</html>

答えて

2

xオブジェクトに残高を持てます。

var app = angular.module("myApp", []); 
 
app.controller("myCtrl", function($scope) { 
 
    $scope.records = [ 
 
    { 
 
     "Amt" : "500", 
 
     
 
    }, 
 
    { 
 
     "Amt" : "800", 
 
     
 
    }, 
 
    { 
 
     "Amt" : "1580", 
 
    }, 
 
    { 
 
     "Amt" : "1122", 
 
    } 
 
    ] 
 
    
 
    $scope.value=function(d, x) 
 
    { 
 
    //How set the value of balace amout by subtrating Amt-Entered Amt. 
 
    //I tried this but in my scenarion, same vallue is setting for all balance column though i make change in only one column 
 
    x.balance = x.Amt - d; 
 
    } 
 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body ng-app="myApp"> 
 

 
<table ng-controller="myCtrl" border="1"> 
 
    <tr> 
 
    <td>Amt</td> 
 
    <td>Balance</td> 
 
    <td>Entered Amt</td> 
 
</tr> 
 
<tr ng-repeat="x in records"> 
 
    <td>{{x.Amt}}</td> 
 
    <td>{{x.balance}}</td> 
 
    <td><input type="text" ng-model="d" ng-change="value(d, x)"></td> 
 
</tr> 
 
</table> 
 

 
</body> 
 
</html>

1

の変化は、コード HTML

更新されていることを確認しても、私のscenarionで、同じvallueは、すべてのバランス列に設定されます
 <div ng-app> 
     <h2>Todo</h2> 
     <div> 
     <table ng-controller="TodoCtrl" border="1"> 
     <tr> 
     <td>Amt</td> 
     <td>Balance</td> 
     <td>Entered Amt</td> 
    </tr> 
    <tr ng-repeat="(key,x) in records"> 
     <td>{{x.Amt}}</td> 
     <td><input type="text" readonly ng-model="balance[$index]"></td> 
     <td><input type="text" ng-model="d" ng-change="value(x,d,$index)"></td> 
    </tr> 
    </table> 
     </div> 
    </div> 

JS

function myCtrl($scope) { 
$scope.balance = {}; 
    $scope.records = [ 
    { 
     "Amt" : "500", 

    }, 
    { 
     "Amt" : "800", 

    }, 
    { 
     "Amt" : "1580", 
    }, 
    { 
     "Amt" : "1122", 
    } 
    ] 

    $scope.value=function(obj,val,key) 
    { 

    $scope.balance[key] = parseInt(obj.Amt) - parseInt(val); 
    //How set the value of balace amout by subtrating Amt-Entered Amt. 
    //I tried this but in my scenarion, same vallue is setting for all balance column though i make change in only one column 
    } 
} 
関連する問題