I have a row in a modal that I want to be able to delete. New ones can also be added.
I have a trimmed down template as follows:
<div class="filter-row" delete-filter>
<div class="form-group>
<div class="delete-filter" ng-click="deleteFilter()"><i class="fa fa-trash"></i></div>
</div>
</div>
And a directive as follows:
.directive('deleteFilter', function() {
return {
restrict: 'A',
link: function(scope, element) {
// Scope function to delete a filter row from the DOM
scope.deleteFilter = function() {
console.log("Deleting");
// Remove the directive element
element.remove();
};
}
};
});
This seems to work well to delete a single filter row. But if a new row is added from this template while a row already exists, I can only delete one of the rows. If I add many rows and want to delete, I can still only delete a single row.
To note: the console log within the function will fire every time a delete row is clicked, whether the row is removed or not.
Any help would be appreciated. I think I am missing something but I can't figure out what.
Edit:
A new filter is added with this function from the controller:
// Function to add a new filter row in the filter modal window
self.addFilterRow = function() {
var newFilterRow = $compile("<filter-row></filter-row>")($scope);
$("#filterForm").append(newFilterRow);
};
Here is a Plunkr that may better describe the issue I'm having: