-2

I recently saw some angular code which was like this

angular
    .module("MYController", function($scope) {}) // ...

and in my tutorial section we were taught to define is like this

angular
    .module("MYController", ["$scope", function($scope) {}]); // ...

my question is what is the difference?

Coder Guy
  • 49
  • 9

1 Answers1

0

Quoting from my previous answer,

After minification the old code wouldn't have worked because of the fact that when UglifyJS (or equivalent) minifies it, it renames the reserved variables $scope, etc. to e a or something else, which Angular is unable to resolve; when you use the array notation (I forgot its specific name), angular knows what to inject (Because you are using array of String and String does not convert at minification time), only the names have changed.

You can read more about it http://toddmotto.com/angular-js-dependency-injection-annotation-process/

Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
weirdpanda
  • 2,521
  • 1
  • 20
  • 31
  • so it makes minification better? – Coder Guy Nov 09 '15 at 09:44
  • Yes. In a way. Read that article, and you'll get to know. – weirdpanda Nov 09 '15 at 09:44
  • Actually its not a kind of minification better. The main cause of minification is to decrease the size of downloadable JavaScript code to increase performance and loading time. Suppose you have written a code function myFunc(abc, xyz){abc+xyz}. Now at the time of minification myFunc will be changed to m, abc to a, xyz to x (Kind of that). So size of the file will get decrease. If same way angular code does this then $scope injection will be converted as s (kind of). So angular will not understand it is $scope of other. But if you use array of some strings, Since string does not change it work – Partha Sarathi Ghosh Nov 09 '15 at 09:53