1

When declaring a controller I usually see these:

University.controller('ClassroomController', function($scope){
//do stuff
});

What's the difference of it compared to:

var classroomController = function ($scope){
//do stuff
}

University.controller('ClassroomController',['$scope',classroomController])

The tutorials doesn't seem to say whats the advantage of the second type of declaration.

user3770093
  • 315
  • 2
  • 11
  • 20
  • 1
    The latter declares the `classroomController` symbol in whatever scope you're in which may not be desirable. See http://stackoverflow.com/questions/8862665/what-does-it-mean-global-namespace-would-be-polluted. It also supports Angular's DI annotation which will survive minification. Functionally, they're identical – Phil Aug 17 '16 at 06:40
  • @Phil Thanks for the reply! but I'm a bit confused with your answer, you say the latter may not be desirable but after reading the link you shared, from what I understood the latter declaration ensures garbage collection. – user3770093 Aug 17 '16 at 06:54
  • Hey @Phil thanks! I understand it now, the second declaration also minifies angular well. – user3770093 Aug 17 '16 at 07:09

1 Answers1

2

The first syntax will break if you minify your js. Angular will for exemple try to find $timeout but will find a

To prevent that, you have to declare explicitly or inline your dependencies

Dan M. CISSOKHO
  • 1,070
  • 1
  • 12
  • 27