1

I have started going through the Angular docs and wondered what is the difference between this controller constructor:

angular.module('myApp', [])
    .controller('SomeController', function() {
      this.qty = 1;
}); 

And this one:

angular.module('myApp', [])
    .controller('SomeController', ['$scope', function($scope) {
       $scope.qty = 1;
}]); 

Which of the two is preferred and why?
What is the added value of the injected $scope object?

html_programmer
  • 18,126
  • 18
  • 85
  • 158

1 Answers1

2

When you are trying to minify the code, at that time

angular.module('myApp', [])
    .controller('SomeController', ['$scope', function($scope) {
       $scope.qty = 1;
}]); 

Would be useful to inject the dependencies properly.

  • 1
    Thanks, but this doesn't answer `$scope` vs `this`; it only explains why to declare '$scope' explicitly as a dependency in the array. I'm already checking it out; thanks for help anyway. – html_programmer Mar 07 '15 at 22:01
  • How does this address the question of whether `$scope` or `this` should be used? – New Dev Mar 07 '15 at 22:02