1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I never heard the name uglifier , that is probably the tool that got to some of my ex's
Closure, Grunt/gulp packages, RequireJS, Webpack...they all have an "uglifier" that renames the codebase to compact it. For instance: npmjs.com/package/gulp-uglify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.