Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  111] [ 4]  / answers: 1 / hits: 23258  / 10 Years ago, wed, june 18, 2014, 12:00:00
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


More From » angularjs

 Answers
35

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.


[#70525] Monday, June 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryankiah

Total Points: 183
Total Questions: 99
Total Answers: 112

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
;