suman goud suman goud - 1 month ago 5
AngularJS Question

How to get calculated values result and add new row

i have three inputs , am subtracting values in two inputs and binding result in result input . but i want to add new row on button click, i tried giving ng repeat in tr but not working

var ReceiptsApp = angular.module('ReceiptsApp', []);

ReceiptsApp.controller('ReceiptsController', function ($scope) {
$scope.rvm = [{}];
$scope.addRow1 = function (index) {
//alert('ss');
$scope.result = $scope.r.val1 - $scope.r.val2;
if ($scope.rvm.length == (index + 1)) {
$scope.rvm.push({
});
}

}


});


find code here

https://jsbin.com/qutuyodite/edit?html,js,output

Answer
ReceiptsApp.controller('ReceiptsController', function ($scope) {
 $scope.rvm = [{}];
   $scope.addRow1 = function (index, r) {
        // pass the particular array object on which you want to perform calculations when the function is called from ng-click and access those particular objects values using r.val1 and r.val2
        //Access particular "rvm" object using index
        $scope.rvm[index].result = r.val1 - r.val2;
        if ($scope.rvm.length == (index + 1)) {
            $scope.rvm.push({
            });
        }

    }

In HTML

<td>
   <input type="text" lass="input-large" ng-model="r.result" name="res" />
  </td>
 <td>
  <button type="button" class="btn btn-default" ng-click="addRow1($index, r)">Add</button>
 </td>

Working JSBin

Comments