0

So I'm a bit confused on why I need to have [] syntax around my scope and http get. Aren't these items already available?

 angular.module('NoteApp').controller('NotesIndexController', function($http, $scope))

vs

 angular.module('NoteApp').controller('NotesIndexController', ['$http', '$scope', function($http, $scope){ ... }]);

Isn't this legal?

 angular.module('Test').controller('TestCtrl', function($scope){ ... });

1 Answer 1

1

It is a common practice for developers to compress their code down to the smallest size before sending it through the network. This is sometimes called minification. When minifying a file, it is common to rename variables to something smaller. (If you've ever stepped through the angular.min.js code, you can see that all the variables are usually one or two letters)

Without using the array syntax, when variable names are changed, Angular has no way of knowing what that variable should have been pointing to. The array syntax helps Angular's dependency injector to deduce which module each varable is referring to.

Here's a great article which explains it well: https://scotch.io/tutorials/declaring-angularjs-modules-for-minification

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

1 Comment

You can also use github.com/mgol/grunt-ng-annotate in your build process, which will add those annotations (array syntax) for you, so you'll keep your non-minified code cleaner.

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.