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


what's difference controller arguments, 1 wrapping services , return function in array,

it seems type 1 obeys dry principle.

i don't meaning of difference between 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) {    ....  }); 

difference how angular find dependencies.

with type 1, angular uses string find dependencies, can have 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 parameter names find dependencies, can't use whatever name like, otherwise, angular may not able find it.

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

dependency annotation

angular invokes functions (like service factories , controllers) via injector. need annotate these functions injector knows services inject function. there 3 ways of annotating code service name information:

  • using inline array annotation (preferred)
  • using $inject property annotation
  • implicitly function parameter names (has caveats)

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -