I have the following two directives. One is a query builder and the other is a query row. The query builder directive uses ng repeat to list query rows from an array. The add button works, however I'd like to include a delete button. However, the problem is I cannot seem to get $index so that I can pass it as an argument to the delete function (i.e. delete($index))
Below is the JS code
.directive('queryBuilder', function() {
return {
restrict: 'E',
scope: {},
controller: function($scope) {
$scope.rows = [{}]
//add method not used - delete in future
$scope.add = function() {
$scope.rows.push({})
}
$scope.$on('addRowRqst', function(evt) {
// evt.stopPropagation()
console.log('add rqst received')
$scope.rows.push({})
});
$scope.$on('deleteRowRqst', function(evt) {
// evt.stopPropagation()
console.log('delete rqst received')
$scope.rows.push({})
});
},
templateUrl: 'queryBuilderTemplate.html',
}
}).directive('queryRow', function() {
return {
scope: {},
restrict: 'EA',
templateUrl: 'queryRowTemplate.html',
controller: function($scope) {
$scope.addRqst = function() {
console.log('addRowRqst click')
$scope.$emit('addRowRqst')
};
$scope.deleteRqst = function(index) {
console.log('deleteRowRqst click')
$scope.$emit('deleteRowRqst')
};
},
link: function(scope, elem, attrs) {
}
}
And here is the HTML code of the query builder template
<form role="form">
<query-row ng-repeat="row in rows track by $index"></query-row>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And here is the Delete button (part of the query row template). The $index here is 'undefined' when I try to pass it as an argument
<button class="btn btn-default" ng-click="deleteRqst($index)" type="submit">Delete Row</button>
Here's the plunker http://plnkr.co/edit/rDkXpIgiOSoNvLNPsVbP
My goal: get a Delete button to work and delete the selected row