2017-02-04 56 views
1

減去值設置列的值下面是snippset,如何從NG-重複值

我想設置餘額列中輸入金額列基於輸入的金額值。

我試過,但我scenarion,雖然我做的改變只有一列

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

相同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 
    } 
}