What is the purpose of square bracket usage in Angular?

angularjs, javascript

Solution

It enables AngularJS code to be minified. AngularJS uses parameter names to inject the values to your controller function. In JavaScript minification process, these parameters are renamed to shorter strings. By telling which parameters are injected to the function with a string array, AngularJS can still inject the right values when the parameters are renamed.

Problem

I would like to understand the difference between the declaration of `MyOtherService` and `MyOtherComplexService`. Especially what is the purpose of square bracket part? When to use them and when not? ``` var myapp = angular.module('myapp', []); myapp.factory('MyService', function($rootScope, $timeout) { return { foo: function() { return "MyService"; } } }); myapp.factory('MyOtherService', function($rootScope, $timeout, MyService) { return { foo: function() { return "MyOtherService"; } } }); myapp.factory('MyOtherComplexService', ['$rootScope', '$timeout', 'MyService', function($rootScope, $timeout, MyService) { return { foo: function() { return "MyOtherComplexService"; } } }]); myapp.controller('MyController', function($scope, MyOtherService, MyOtherComplexService) { $scope.x = MyOtherService.foo(); $scope.y = MyOtherComplexService.foo(); }); ```

Original source