1

So I was using this website to show me the basics: https://www.codeproject.com/Articles/1130132/Learn-AngularJS-for-Beginners

and on the scope inheritance section I was thrown off because of the controller used for that example compared to the example controller used as intro to controllers.

what is the difference between:

app.controller("PersonController", function($scope) {
    $scope.employeeData = personData;
    $scope.employeeMethod = function() {
      console.log("Hello, I am an Employee");
    }
});

and

app.controller("MainController", ['$scope', function($scope){
    $scope.name = "Donald"; 
    $scope.color = "White";
}]);

as in the first one is name then function, the second is name then bracket.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Help
  • 9
  • 2
  • The difference is for minification. Check this out https://scotch.io/tutorials/declaring-angularjs-modules-for-minification – scniro Jun 12 '17 at 23:02
  • From the source is OP, "This way of injecting dependencies is called ‘Dependency Injection" and what @scniro said – Harry Jun 12 '17 at 23:03
  • 1
    See [AngularJS Developer Guide - Dependency Injection](https://docs.angularjs.org/guide/di). – georgeawg Jun 12 '17 at 23:40
  • See also [Angular Module Minification Bug](https://stackoverflow.com/questions/17238759/angular-module-minification-bug) – georgeawg Jun 12 '17 at 23:57

1 Answers1

0

from here implicit annotation assumes function params to be name of the services

someModule.controller('MyController', function($scope, greeter) {
  // ...
});

$scope and greeter are the names of services above.

So both DI techniques work the same way except when some JavaScript minifiers/obfuscators are used because they can rename function parameters but not the inline Array.

Shivam Tiwari
  • 345
  • 3
  • 11