3

I'm a beginner in Angular development. I don't know why we inject twice argument inside for controller like:

app.controller('mycontroller', ['$scope', 'myFactory', 'Myothers', function ($scope, myFactory, Myothers) {}])

and see

app.controller('mycontroller', function ($scope, myFactory, Myothers) {})

Could you explain why we do this?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
skvj
  • 103
  • 1
  • 2
  • 9
  • Its for minification. Angular hacks up named arguments by inferring the service you want from the variable name. When you minify your code your arguments get renamed. So when angular runs it can't find the correct services. This allows you to minify your code and angular still know which service your trying to inject. – ste2425 Jan 25 '17 at 07:43
  • See [AngularJS Developer Guide - Dependency Annotation](https://docs.angularjs.org/guide/di#dependency-annotation). – georgeawg Jul 03 '17 at 15:29
  • @ste2425, I'm having an issue trying to inject parameters into an inline controller function. Any chance you could take a look at [it](https://stackoverflow.com/questions/54930202/having-trouble-properly-injecting-parameters-into-inline-controller-function-in)? – Ryan Mar 01 '19 at 21:08

3 Answers3

6

The reason is to protect the code from javascript minification.

The $inject makes sure that the variable names are preserved in the form of strings.

So ideally your app code should look something like this:

 var app = angular.module('YourApp', []);
 var appCtrl = app.controller('AppCtrl', AppCtrl);

 appCtrl.$inject = ['dep1', 'dep2']; //add all the dependencies

 function AppCtrl (dep1,dep2){  //add the name of the dependencies here too
    //your controller logic
 }

During minification javascript replaces variable name with custom names, so dep1 might be replaced by d and hence will cause error.

But $inject will let angular know that the actual name of the dependency is dep1 as it is stored in the form of string value which is protected from minification.

Hence we use $inject.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
3

first you can do without array like:

app.controller("myController",function($scope,myFactory,MyOrders){});

in array you are declares variables , you can do like something that:

app.controller('mycontroller',['$scope', 'myFactory', 'Myothers', function (s, f, o) {}])

the s as scope , the f as myfactory , the o as order;

it is your's choice how to use , but in angular tutorials they say the right way is :

app.controller('mycontroller',['$scope', 'myFactory', 'Myothers', function (s, f, o) {}])
omer cohen
  • 272
  • 1
  • 4
  • 19
2

When we pass a dependency as an Array Argument, the application does not break in production when we minify the application.

Ways to do this :

  • Using the Named function
  • Using the Inline Anonymous function

Using the Named function :

We can pass dependencies as Array Arguments with the named function.

var app = angular.module('app', []);

function MyCtrl($scope) {

    $scope.name = "Rohit";

};

app.controller('MyCtrl', ['$scope', MyCtrl]);

Using the Inline Anonymous function :

var app = angular.module('app', []);

app.controller('MyCtrl', ['$scope', function ($scope) {

    $scope.name = "Rohit";

}]);

Differences :

The difference is that when the app.controller('mycontroller', function ($scope, myFactory, Myothers) {}) is minified, the parameter name will be minified and angular will no longer be able to figure out which dependencies to inject. The array syntax with the dependency in a string means that it is minification safe.

alternate solution :

we can use ng-annotate library which will change the app.controller('mycontroller', function ($scope, myFactory, Myothers) {}) into the app.controller('mycontroller', ['$scope', 'myFactory', 'Myothers', function ($scope, myFactory, Myothers) {}]) so that the code is again minification safe.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123