如何通过从ng-repeat值中减去值来设置列值

时间:2017-02-04 11:19:50

标签: angularjs angularjs-ng-repeat

下面是snippset,

我想根据输入的Amt in Entered Amt列设置余额列的值。

我尝试了这个但是在我的观点中,虽然我只在一列中进行了更改,但是相同的vallue正在设置所有平衡列



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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您可以在@AfterViews void afterViews() { if (uri != null) { displayFromUri(uri); } else { displayFromAsset(SAMPLE_FILE); } setTitle(pdfFileName); MobileAds.initialize(getApplicationContext(), "app-id"); AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } 对象中获得余额。

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;
  }
});

答案 1 :(得分:1)

这是更新的代码    HTML

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