0

I am not aware of latest release of angularjs. I saw a piece of code like below

angular.module('app')
    .controller('myApp', [myApp]);

function myApp() {
      var vm = this;
      vm.title = 'Customers';
      vm.customers = [
        {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
        ];
    };

instead of code i used to write earlier like

angular.module('app')
    .controller('myApp', function($scope){
      $scope.title = 'Customers';
      $scope.customers = [
        {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
        ];
});

So why are we injecting the functionality required for the controller? what advantage does it provide. Correct me if my understanding is wrong.

2 Answers 2

1

That controller is not being used in its own dependency injection.

This is simply a syntax for defining a controller:

.controller('controllerName', [
    'dependency1', 
    'dependency2', 
    function (dependency1, dependency2) {
    }
]);

This approach allows specifying the names for the dependencies explicitly and prevents your dependencies from getting messed up if you minify the code.

In the case of your example, there are no dependencies, and the function is being referenced as a variable instead of being included inline.

It's also using the "controller as" syntax (where the controller refers to itself as this) instead of using the $scope service, but that is a completely separate matter.

You could also modify your second example to work the same way:

angular.module('app')
    .controller('myApp', ['$scope', myApp]);

function myApp($scope){
      $scope.title = 'Customers';
      $scope.customers = [
        {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
        ];
}
Sign up to request clarification or add additional context in comments.

Comments

0

What it does is instead of calling an anonymous function from controller its separate the function from the controller and call the function.In this case, function name is myApp

reason to call it with square brackets is when you are using angular minify version, dependency injector unable to identify the dependency so it produces an error.

In order to identify the relevant dependency by the injector, angular allow you to write dependency as string array when you are initializing controller. Since there is no dependency in this example only empty square brackets appear.

If you have a dependency, let's say $scope then you need to inject it like this

angular.module('app')
    .controller('myApp', ['$scope',myApp]);

function myApp($scope) {
      var vm = this;
      vm.title = 'Customers';
      vm.customers = [
        {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
        ];
    };

This also can be done using $inject also

angular.module('app')
        .controller('myApp',myApp);

    myApp.$inject = ['$scope'];

    function myApp($scope) {
          var vm = this;
          vm.title = 'Customers';
          vm.customers = [
            {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
            ];
        };

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.