I am struggling with implementing a filter inside a directive. I have a basic filter that works fine within an ng-repeat directive, but I'm unsure of how to apply it within a directive's link function. Below is the code for my directive where I am attempting to pass the filter as a function. Is this the correct approach? And if not, what would be a better way to achieve this?
JavaScript
myApp.directive('repeatDirective', ['$filter', function($filter) {
return {
scope: {
'filter': '&?',
'itemList': '='
},
template: "<div ng-repeat='item in itemList'>{{item .name}}</div>",
link: function (scope,element,attrs, ngModelCtrl) {
**// How can I implement the filter on itemList here???**
}
};
}]);
filter
$rootScope.collectionFilter= function (transType) {
if($scope.formData_EventDetails.actualPotential){
if($scope.formData_EventDetails.actualPotential=='NM'){
//console.log(transType.tranTypeName.indexOf('Near Miss'));
return transType.tranTypeName.indexOf('Near Miss') >=0 ;
}
else{
return transType.tranTypeName.indexOf('Near Miss') <0 ;
}
}
return true;
};
HTML
<repeat-directive item-list="someObjectCollection" filter="collectionFilter()">
</repeat-directive>