Angularjs自定义过滤器不能被注射不明供应商 [英] Angularjs custom filter can't be injected, unknown provider
问题描述
我想创建一个自定义过滤器,但是当我尝试把它注入到我的控制器,我得到未知提供商。我已经检查和双重检查所有的引用,但我看不出有什么不对。
I'm trying to create a custom filter, but when I try to inject it into my controller, I get Unknown provider. I have checked and double checked all the references, but I can't see what's wrong.
我知道该文件在我的index.html正确地引用,它被加载并可以通过检查发现。这是code我有:
I know that the file is referenced in my index.html correctly, it is loaded and can be found by the inspector. This is the code I have:
在我的app.js:
angular.module('equiclass', ['equiclass.controllers',
'equiclass.services',
'ngRoute'])
.config(function ($routeProvider) {
$routeProvider
.when('/courses', {
templateUrl: 'views/courses.html',
controller: 'CourseCtrl'
// And some other stuff with routes
});
angular.module('equiclass.controllers', ['equiclass.services', 'equiclass.filters']);
angular.module('equiclass.services', []);
angular.module('equiclass.filters', []);
我的过滤器:
angular.module('equiclass.filters')
.filter('testFilter', function() {
return function(input) {
return undefined;
};
});
和控制器:
angular.module('equiclass.controllers')
.controller('CourseCtrl', function ($scope, testFilter) {
});
当然很简单,但它根本不起作用,我不明白为什么。我做了一些服务,他们所有的工作,并很好地一起玩。
Of course quite simplified, but it simply doesn't work, and I can't see why. I have done several services and they all work and play along nicely.
推荐答案
您并不需要注入过滤器本身。
You don't need to inject the filter itself.
这code ...
angular.module('equiclass.controllers')
.controller('CourseCtrl', function ($scope, testFilter) {
});
应
angular.module('equiclass.controllers')
.controller('CourseCtrl', function ($scope) {
});
和里面的 CourseCtrl
你应该用你的过滤器平时一样。
And inside CourseCtrl
you should use your filter as you normally do.
注射模块'equiclass.filters
到您的模块'equiclass.controllers
就足够了。
Injecting the module 'equiclass.filters'
into your module 'equiclass.controllers'
is enough.
我也有类似的问题,发了一个帖子关于它的<一个href=\"http://www.seangwright.me/blog/development/2014/09/18/angular-js-custom-filters-separate-modules\"相对=nofollow>在我的博客。
I had a similar issue and made a post about it on my blog.
- 编辑
由于 n00dl3 提到下面的棘手的部分是自动命名惯例的角度是如何工作的。如果你的名字你的过滤器 specialNumberFilter
然后当你注入它,你需要将它称为的 specialNumberFilterFilter
。这允许您使用的过滤器作为一个函数,这是它是什么。
As n00dl3 mentions below the tricky part is how the auto-naming convention works in Angular. If you name your filter specialNumberFilter
then when you inject it you need to refer to it as specialNumberFilterFilter
. This allows you to use the filter as a function, which is what it is.
//在控制器
vm.data = specialNumberFilterFilter(vm.data,'一个');
不过,我相信你也可以使用过滤器,而不将其注入控制器如果在一个正在被评估的字符串前pression使用,比方说,一看表,因为这将是一样的情景时,你正在使用它的模板。
But I believe you can also use the filter without injecting it into a controller if it is used in a string expression that is being evaluated by, say, a watch because this would be the same as the scenario when you are using it in a template.
//里面的手表 - 没有控制器所需的注射
`$ $范围的手表。('vm.data | specialNumberFilter',函数(新,旧){...})`
这篇关于Angularjs自定义过滤器不能被注射不明供应商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!