JohnGalt
JohnGalt

Reputation: 2881

The impact of creating a controller function with and without angular.module().controller() in angular?

In the Angular documentation there are two examples of creating a controller:

function GreetingController($scope) {
  $scope.greeting = 'Hola!';
}

and

var myApp = angular.module('myApp',[]);

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

Both are used in the markup:

<div ng-controller="GreetingController">
  {{ greeting }}
</div>

My question is, what are the advantages of using the angular.module().controller() method?

Upvotes: 0

Views: 41

Answers (1)

Amir Popovich
Amir Popovich

Reputation: 29836

A controller is basically a function. In the first case you can reuse the function over all your applications(global scope function) and not bind it to one.

The second case actually attaches the controller to an application(application scope), allowing you to pass not only a function but an array that includes strings in case you want to minify your javascript files.

Upvotes: 3

Related Questions