打字稿:如何在自定义过滤器中使用角度$ filter

时间:2023-01-06 19:40:24

How can I utilize angular $filter within a custom filter? How to inject $filter dependency?

如何在自定义过滤器中使用角度$ filter?如何注入$ filter依赖?

module Filters {

    export class CustomFilter {
        public static Factory() {
            return function (input:<any>) {

                var result = [];

                //Would like to utilize $filter.('filter') here

                return result;
            }
        }
    }

    angular.module('app')
        .filter('customFilter', [CustomFilter.Factory]);
}

1 个解决方案

#1


7  

You could just inject $filter and use type ng.IFilterService or specifically use filterFilter (if you are looking for $filter('filter')) and use type Function.

您可以注入$ filter并使用类型ng.IFilterService或专门使用filterFilter(如果您正在寻找$ filter('filter'))并使用类型Function。

module Filters {
    export class CustomFilter {
        //Here use `$filter` or filterFilter itself
       //public static Factory($filter: ng.IFilterService)
        public static Factory(filter:Function) {
            return function (input:any) {

                var result = [];

                //filter is now $filter('filter')

                return result;
            }
        }
    }

    angular.module('app')
        .filter('customFilter', ['filterFilter', CustomFilter.Factory]);
        //Inject $filter for generic filter getter
} 

#1


7  

You could just inject $filter and use type ng.IFilterService or specifically use filterFilter (if you are looking for $filter('filter')) and use type Function.

您可以注入$ filter并使用类型ng.IFilterService或专门使用filterFilter(如果您正在寻找$ filter('filter'))并使用类型Function。

module Filters {
    export class CustomFilter {
        //Here use `$filter` or filterFilter itself
       //public static Factory($filter: ng.IFilterService)
        public static Factory(filter:Function) {
            return function (input:any) {

                var result = [];

                //filter is now $filter('filter')

                return result;
            }
        }
    }

    angular.module('app')
        .filter('customFilter', ['filterFilter', CustomFilter.Factory]);
        //Inject $filter for generic filter getter
}