[英]How to Use Filter Passed from Controller in Directive Link Function
I have a filter that i would like to use inside a directive. 我有一个过滤器,我想在指令中使用。 I have a simple filter which works fine inside ng-repeat directive I am not quite sure how exactly I can apply inside directive link function. 我有一个简单的过滤器,在ng-repeat指令内工作正常我不太确定我是如何在指令链接函数中应用的。 I wrote below directive, taking the filter as a function, is this correct approach, how can i do it this way? 我写下面的指令,把过滤器作为一个函数,这是正确的方法,我怎么能这样做? or is there better way of doing it? 或者有更好的方法吗?
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 to filter itemList here???**
}
};
}]);
$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;
};
<repeat-directive item-list="someObjectCollection" filter="collectionFilter()">
</repeat-directive>
Define your filter with Angular's register filter method, then use as you would use built-in filters. 使用Angular的寄存器过滤器方法定义过滤器,然后像使用内置过滤器一样使用。 Let me know if you have questions. 如果您有疑问,请告诉我。
Filter: 过滤:
myApp.filter('collectionFilter', function(){
return function(input){
return input ? 'true : 'false';
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.