Angular syntax: why […] is used when declaring a controller? [duplicate]

This question already has an answer here:

Previously, when I declared a controller, I wrote:

  .controller('MyCtrl', function($scope, $http) {
    $scope.phones = $http.get(...);

And the controller is able to be loaded.

However, today when I used $resources instead of http wrote as previously without the […] part, I received something like “MyCtrl is not a function, undefined” and it worked after I changed to:

  .controller('MyCtrl', ['$scope', 'Phones', function($scope, Phones) {
    $scope.phones = Phones.query();

I have made sure that I included all the necessary injections for ngResources, $resources as well as my services.js file that defines the Phones $resource service.

My question is: why is the […] part necessary? I read the documentation and they include it as well but I don’t understand why it is necessary because in some cases the controller still works even without that […].

Source: syntax

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.