AlexStack
AlexStack

Reputation: 17381

In angular modules why the body is the last array element?

This is more of an architectural question.

One of the most common forms of defining an angular module is this:

angular.module('app', [])
  .controller('Ctrl', ['$scope', function Ctrl($scope) {
    //body...
}]);

But I don't find the syntax very intuitive. How about having the list of dependencies in an array like AMD:

angular.module('app', [])
  .controller('Ctrl', ['$scope'],
          function Ctrl($scope) {
            //body...
          });

This way the whole array will contain only string elements each of which refers to a module. The array matches the function parameters one by one. (kinda like arguments).

So my question is why Angular designers went for this convention?

Upvotes: 2

Views: 52

Answers (1)

KernelCurry
KernelCurry

Reputation: 1297

It kind of does that in a sense. you can do this by using $inject.

function SomeCtrl ($scope) {
  // do something with $scope
}

SomeCtrl.$inject = ['$scope'];

angular
  .module('app', [])
  .controller('SomeCtrl', SomeCtrl);

I am not a no Expert on this, but I did found a great post on how this process works and it might help answer your question: http://toddmotto.com/angular-js-dependency-injection-annotation-process/

Upvotes: 3

Related Questions