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

时间:2015-10-21 16:28:57

标签: javascript angularjs dependency-injection typescript

如何在自定义过滤器中使用角度$ 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 个答案:

答案 0 :(得分:7)

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

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
}