Search code examples
javascriptangularjsangularjs-scopeangularjs-controllerangularjs-controlleras

Angular controller functions are both constructors and a decorators?


If one writes a controller as ctrl for the following code:

function Controller($scope) {
  $scope.abc = "123";
}

angularApp.controller("controller", Controller);

Does the pseudo code below accurately describe how $scope differs from controller?

$scope = $rootScope.$new();
controller = new Controller($scope);
$scope.ctrl = controller;

My question is:

  • Is the above pseudo code an accurate mental model of how a scope differs from a controller object?
  • Is function Controller(){} both a constructor function (for controller objects) and a decorator (for scope objects) at the same time?

Solution

  • Your assumption is correct. Scope is dumb- for many reasons. That's why they're fading it out and it won't exist in angular 2. It's recommended that you don't use scope ever but only for watch, eval, and events.