[英]“Unknown Provider” when injecting custom filter into service
Im trying to filter the results of my search application with a custom filter. 我正在尝试使用自定义过滤器过滤搜索应用程序的结果。 Now, the angular documentation states that you should avoid defining stateful filters. 现在,角度文档指出您应该避免定义有状态过滤器。 My workaround was building a service which handles the filter inputs and custom filter which takes these inputs as an argument object. 我的解决方法是构建一个处理过滤器输入的服务和将这些输入作为参数对象的自定义过滤器。
This concept worked fine while I had an implementation which did this just in the service with a custom filtering function. 当我有一个实现在服务中使用自定义过滤功能执行此操作时,此概念运行良好。 Now I moved it over to a filter declaration, like this: 现在,我将其移至过滤器声明,如下所示:
(function () {
'use strict';
angular.module('searchApp.search')
.filter('referenceFilter', function () {
return function (references, filterModel) {
var filteredReferences = [];
/* filtering */
return filteredReferences;
}
});
})();
Now when I am trying to inject the filter into my service I get the "Unknown Provider"-Error stating that it cant find a Provider for my referenceFilter. 现在,当我尝试将过滤器注入到服务中时,出现“未知提供程序”错误,指出它无法为我的referenceFilter找到提供程序。 Although I think its injected properly and defined before the service. 虽然我认为它已正确注入并在服务之前定义。
(function () {
'use strict';
angular.module('searchApp.search')
.factory('referenceFilterService', ['referenceFilter', referenceFilterService]);
function referenceFilterService(referenceFilter) { // 'reference' | 'referenceFilter' doesnt make a difference
....
/* filter call */
function getFilteredReferences(references) {
return referenceFilter(references, filterModel);
}
....
}
})();
<script src="search/search.module.js"></script>
<script src="search/filter/referenceFilter.filter.js"></script>
<script src="search/filter/referenceFilter.service.js"></script>
What am I doing wrong? 我究竟做错了什么?
You should not inject your filter directly, use the built-in $filter
service instead: 您不应直接注入过滤器,而应使用内置的$filter
服务:
angular.module('searchApp.search')
.factory('referenceFilterService', ['$filter', referenceFilterService]);
function referenceFilterService($filter) {
var referenceFilter = $filter('referenceFilter');
// etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.