On Sun, Feb 3, 2013 at 7:30 PM, David Simmons <
shortl...@gmail.com> wrote:
> Do you know why the Angular tutorial uses new? It would seem a strange
> decision if it is not the correct way to create a controller.
Well, technically controllers are created with "new" plus some
additional magic and DI:
https://github.com/angular/angular.js/blob/master/src/auto/injector.js#L590
So, if your only dependency is a $scope and you don't need any other
dependencies you could use "new" in tests.
The trouble is that then you need to always think: do my controller
need any additional dependency? Should I inject things by hand or let
AngularJS inject things etc.
The other problem with using the "new" keyword is that controllers
defined on a module can't be instantiated like this since you don't
have access to a globally defined constructor function. As soon as you
start defining controllers on a module (and I think you should:
http://stackoverflow.com/q/13362921/1418796) then using "new" is
simply not an option.
What I was trying to say that if you are looking for a "fast" rule
just use the $controller service as it will always work and will
inject services for you.
Of course if you know how controllers are instantiated and what to do
it "by hand" - it is OK.
Not sure why tutorial is using globally defined controllers and the
"new" keyword - I guess it is to keep things as simple as possible and
not to overload people with too much details from the start.
Cheers,