$filter is not a function AngularJS
Asked Answered
I

3

14
app.controller('myController', ['$scope', '$http', '$filter', function($scope, $http, $filter) {

The above is an example of my code where I am trying to use $http.get and also $filter inside my controller.

The only problem is when I use it like so, the console log throws an error that says $filter is not a function.

app.controller('myController', ['$scope', '$http', '$filter', function($scope, $filter, $http) {

When I swap them round it throws an error that $http is undefined

Immerge answered 18/6, 2014 at 11:43 Comment(4)
You have to declare $filter in the dependency list, i.e. ['$scope', '$http', '$filter', function($scope, $http, $filter) {...Epididymis
@NikosParaskevopoulos Sorry I forgot to add that into the example, it shows the error with this in the dependency list.Immerge
OK; there is something really fishy here. This code should work. Can you reproduce the problem in a fiddle/plunk?Epididymis
How did you finally resolve this issue? I'm having the same problem and the answer marked below isn't working. I have the order correctly in placeWelkin
C
25

When you are using

app.controller('myController', ['$scope', '$http', '$filter', function($scope, $filter, $http) {

variable $filter is actually a instance of $http, and $http is instance of $filter. Actually it doesn't matter what is written in function(...) params.

What is important here, is the order of injectibles you are using, for example

app.controller('myController', ['$scope', '$http', '$filter', function(a, b, c) {

will map to instances:

  • a -> scope
  • b -> $http
  • c -> $filter

From angular docs:

Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

So by using array notation for yout controller, you are making sure that code will work after code is minified.

Comply answered 18/6, 2014 at 11:56 Comment(2)
dude.. you saved me.. Thank youMola
while not the exact scenario, conflicting with http, I still found that changing the order fixed my issueCropeared
A
2

add filter after http and angular version also defends how you are going to use filter.

     plateFormController.$inject = ['$scope', '$http',
    '$filter','$timeout', '$q', '$mdSidenav', '$log'];



function plateFormController($scope, $http,$filter, $timeout, $q) {
          jsonByName=$filter('filter')($scope.json, { name: 'a' });
        }
Aegaeon answered 31/1, 2017 at 10:22 Comment(0)
B
0

In my case, expected data List object is null, by ensuring that expected List Object not null. I am able to avoid this error.

Blida answered 28/11, 2017 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.