user3675188
user3675188

Reputation: 7409

what's the difference for the controller arguments, one is wrapping services and return function in a array

what's the difference for the controller arguments, one is wrapping services and return function in a array,

It seems type 1 obeys the DRY principle.

I don't get the meaning of the difference between the following 2 syntax?

Type 1

app.controller("UserCtrl", ['$scope', '$http', '$resource', 'Users', 'User', '$location', function($scope, $http, $resource, Users, User, $location) {
    ....
}]);

Type 2

app.controller("UserCtrl", function($scope, $http, $resource, Users, User, $location) {    .... 
});

Upvotes: 0

Views: 17

Answers (1)

sdfacre
sdfacre

Reputation: 1273

Difference is how Angular find the dependencies.

With type 1, Angular uses the string to find dependencies, so you can have your own parameter names.

i.e.

app.controller("UserCtrl", ['$scope', '$http', '$resource', 'Users', 'User', '$location', function(s, h, r, us, u, l) {
....
}]);

With type 2, Angular uses the parameter names to find dependencies, you can't use whatever name you like, otherwise, Angular may not be able to find it.

More on https://docs.angularjs.org/guide/di

Dependency Annotation

Angular invokes certain functions (like service factories and controllers) via the injector. You need to annotate these functions so that the injector knows what services to inject into the function. There are three ways of annotating your code with service name information:

  • Using the inline array annotation (preferred)
  • Using the $inject property annotation
  • Implicitly from the function parameter names (has caveats)

Upvotes: 1

Related Questions