Kévin Duguay
Kévin Duguay

Reputation: 761

AngularJS - What is the correct syntax to initialize a controller?

I have been following a few tutorials about AngularJS and I noticed that there are multiple methods to initialize a Controller.

For example, the following code is based on the AngularJS documentation:

angular.module('todoList', [])
.controller('todoListCtrl', ['$scope',
    function ($scope) {
        ...
    }
]);

However, this code also works:

angular.module('todoList', [])
.controller('todoListCtrl',
    function ($scope) {
        ...
    }
);

Is one method preferred over the other?

Upvotes: 1

Views: 32

Answers (1)

David L
David L

Reputation: 33873

The second syntax is not minification safe. Once you minify, the uglifier will rename $scope meaning that it will not be able to be injected properly by the AngularJS injector, since the injector uses name-matching to identify which depedency to resolve.

The first syntax was added to fix this issue and is the syntax you should use for production applications.

Upvotes: 1

Related Questions