简体   繁体   English

如何在指令链接函数中使用从控制器传递的滤波器

[英]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? 或者有更好的方法吗?

JavaScript 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 to  filter 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 HTML

<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.

相关问题 从指令调用控制器功能(具有从指令传递的参数) - Call Controller Function from Directive (with params passed from directive) 如何访问从父指令传递但在控制器中定义的子指令中的功能? - How can I access the function in child directive which passed from parent directive but defined in controller? 通过指令链接功能监视控制器范围 - Watch on controller scope from directive link function 从链接功能修改指令控制器变量 - Modify directive controller variable from link function 从嵌套指令中的链接函数调用控制器函数 - Call Controller Function from link function in Nested Directive 如何将数据从自定义指令视图传递到控制器? - How data is passed from custom directive view to controller? 链接功能和控制器功能如何在角度指令中共享知识? - How can a link function and controller function share knowledge in an angular directive? AngularJS从指令链接函数到控制器的Pass值 - AngularJS Pass value from directive link function to controller 链接函数内部带有参数的指令的角度调用控制器方法 - Angular calling controller method from directive with parameters inside link function Angular JS:如何在angular指令中从控制器调用链接内的函数 - Angular JS: How to call a function inside link from controller in angular directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM