I would like to know, how to pass params to function dynamically inside AngularJS template. I have many rows in table and each row has a button Add deposit. I would like to pass row number to this button's ng-click function.
Here is snippet of my template:
<div id="documentsTable" class="table-responsive">
<table class="table">
<thead>
...
</thead>
<tbody ng-repeat="document in data">
<tr>
...
<td>
...
<button ng-show="document.depositCheckbox" ng-click="addDeposit({id: document.number})" id="addDepositBtn"
type="submit" class="btn btn-primary">Add deposit</button>
</td>
</tr>
</tbody>
</table>
Directive:
.directive('documentDirective', ['$compile', '$templateCache', function () {
return {
templateUrl: 'templates/documentTemplate.html',
scope: {
data: '=',
addDeposit: '&'
},
restrict: 'E'
}}]);
And in my HTML file I have:
<document-directive data="documents" add-deposit="addDeposit()"></document-directive>
Function addDeposit in my controller:
$scope.addDeposit = function(documentId) {
for(var i=0; i<$scope.documents.length; i++) {
if($scope.documents[i].number == documentId) {
var depositLength = $scope.documents[i].deposit.length;
var deposit = {'number': depositLength, 'value': 0, 'paymentDate': new Date(), 'firstRow': false};
$scope.documents[i].deposit.push(deposit);
break;
}
}
}
Thanks!