0

I am using angular js. I want to get value from input inside a row in table.

Below there is an input element. When i click the button (add) i need to get the value for that particular row.

view

   <tr ng-click="" ng-repeat='customer in customers | filter:query | orderBy: sort.field : sort.order'>

       <td ng-repeat='field in fields'>
           {{customer[field]}}
       </td>
       <td mult-ecs>
           <p class="input-group">
                <span class="input-group-addon">Rs</span>
                <input class="form-control" name="debit" type="text" ng-model="customer['id'].value">
           </p>
           <button ng-click="addMulEcs(customer['id'])" class="btn btn-primary btn-sm" >Add</button>
           <button ng-click="removeMulEcs(customer[id])" class="btn btn-danger btn-sm" >Remove</button>
        </td>
    </tr>

controller

.directive('multEcs', ['$http', function($http){
    return{
        restrict: 'A',
        replace:false,
        scope:{
        },
        link: function(scope, elem, attr){
            scope.addMulEcs = function(id){

            }
        }
    }


}]);

image of table enter image description here

Abel Raj
  • 76
  • 8

1 Answers1

0

You can refer to controller scope's variable by binding it to directive scope. In your case I think one way binding just suit your situation.

In your directive setting:

scope:{
          localEcsValue : "@ecsValue"
    },

And in your template :

<td mult-ecs ecs-value="customer['id'].value">

This will bind scope.localEcsValue to customer['id'].value in controller.

And in the link function you can do :

scope.addMulEcs = function(id){
            alert( scope.localEcsValue() )
        }

Be careful with using ng-model inside directive. As directive will create isolated scope and cause the ng-model not bind to the correct variable. check this for more info

Community
  • 1
  • 1
Hereblur
  • 2,084
  • 1
  • 19
  • 22