如何在自定义过滤器中使用 Angular $ filter?如何注入(inject)$ 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]);
}

最佳答案

您可以只注入(inject)$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
}

09-25 16:12