AngularJS 1.x custom filter can't be injected, unknown provider -
i'm trying create custom filter, when try inject controller, 'unknown provider' error. have checked , double checked references, can't see what's wrong.
i know file referenced in index.html correctly, loaded , can found inspector. code have:
in app.js:
angular.module('equiclass', ['equiclass.controllers', 'equiclass.services', 'ngroute']) .config(function ($routeprovider) { $routeprovider .when('/courses', { templateurl: 'views/courses.html', controller: 'coursectrl' // , other stuff routes }); angular.module('equiclass.controllers', ['equiclass.services', 'equiclass.filters']); angular.module('equiclass.services', []); angular.module('equiclass.filters', []); my filter:
angular.module('equiclass.filters') .filter('testfilter', function() { return function(input) { return undefined; }; }); and controller:
angular.module('equiclass.controllers') .controller('coursectrl', function ($scope, testfilter) { }); of course quite simplified, doesn't work, , can't see why. have made several services , work , play along nicely.
you don't need inject filter itself.
this code...
angular.module('equiclass.controllers') .controller('coursectrl', function ($scope, testfilter) { }); should
angular.module('equiclass.controllers') .controller('coursectrl', function ($scope) { }); and inside coursectrl should use filter do.
injecting module 'equiclass.filters' module 'equiclass.controllers' enough.
i had similar issue , made post on blog.
--edit
as n00dl3 mentions below tricky part how auto-naming convention works in angular. if name filter specialnumberfilter when inject need refer specialnumberfilterfilter. allows use filter function, is.
// in controller vm.data = specialnumberfilterfilter(vm.data, 'a');
but believe can use filter without injecting controller if used in string expression being evaluated by, say, watch because same scenario when using in template.
// inside watch - no controller injection required `$scope.$watch('vm.data | specialnumberfilter', function(new, old) { ... })`
Comments
Post a Comment