28

According to AngularJS's tutorial, a controller function just sits within the global scope.

http://docs.angularjs.org/tutorial/step_04

Do the controller functions themselves automatically get parsed into an encapsulated scope, or do they dwell within the global scope? I know that they are passed a reference to their own $scope, but it appears that the function themselves are just sitting in the global scope. Obviously this can cause problems down the road, and I have learned through experience and education to encapsulate Further more, if they do dwell within the global scope, would it not be considered a best practice to encapsulate them within an object to be referenced like this:

    Object.functionName();

Rather than this:

    functionName();

So as to prevent issues that occur with the pollution of the global scope (ie overriding functions, etc..)

pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286
Andrew Rhyne
  • 5,060
  • 4
  • 28
  • 41

2 Answers2

53

AngularJS supports 2 methods of registering controller functions - either as globally accessible functions (you can see this form in the mentioned tutorial) or as a part of a modules (that forms a kind of namespace). More info on modules can be found here: http://docs.angularjs.org/guide/module but in short one would register a controller in a module like so:

angular.module('[module name]', []).controller('PhoneListCtrl', function($scope) {

  $scope.phones = [..];

  $scope.orderProp = 'age';
});

AngularJS uses a short, global-function form of declaring controllers in many examples but while this form is good for quick samples it rather shouldn't be used in real-life applications.

In short: AngularJS makes it possible to properly encapsulate controller functions but also exposes a simpler, quick & dirty way of declaring them as global functions.

pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286
  • Thanks! This is exactly the type of explanation I was looking for. Keep up the good work! – Andrew Rhyne Nov 13 '12 at 15:37
  • 1
    There are tons of things should be mentioned in the docs lol. I guess this is the kinda the fun part of learning AngularJS, just like treasure hunting. – maxisam Nov 13 '12 at 16:46
  • The Docs are inadequate because it's still new; atleast thats what I am assuming. I really hope they begin writing thorough documentation because there is a whole ton missing from their docs. They don't have all of the available functions listed in the API! – Andrew Rhyne Nov 13 '12 at 22:00
  • Does this also get around the minification issues? – Neil Jan 07 '13 at 16:06
  • 1
    No, minification concerns are orthogonal. For minification you need to use the array-style notation: http://docs.angularjs.org/guide/di and the code example here: https://github.com/angular-app/angular-app/blob/master/client/src/app/projects/projects.js#L16 – pkozlowski.opensource Jan 08 '13 at 14:49
  • @pkozlowski.opensource, how does angular parse the globally accessible function to specific module? – jason Jul 30 '13 at 07:43
19

You can register a controller as part of a module, as answered by pkozlowski-opensource.

If you need minification you can simply extend this by providing the variable names before the actual function in a list:

angular.module('[module name]', []).
  controller('PhoneListCtrl', ['$scope', function($scope) {

    $scope.phones = [..];
    $scope.orderProp = 'age';
  }]);

This will work the same after "minification":

angular.module('[module name]', []).
  controller('PhoneListCtrl', ['$scope', function(s) {

    s.phones = [..];
    s.orderProp = 'age';
  }]);

This notation can be found under "Inline Annotation" at Dependency Injection.

To test a controller, that has been registered as part of a module, you have to ask angular to create your controller. For example:

describe('PhoneListCtrl test', function() {
  var scope;
  var ctrl;

  beforeEach(function() {
    module('[module name]');
    inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
      ctrl = $controller('[module name]', {$scope: scope});
    });
  });

  it('should be ordered by age', function() {
    expect(scope.orderProp).toBe('age');
  });

});

This method of testing the controller can be found under "Testing Controllers" at Understanding the Controller Component.

Community
  • 1
  • 1
user1
  • 536
  • 5
  • 8