Reputation: 1461
I'm just starting to look at Angular, but having a hard time wrapping my head around the need for $scope
. Javascript already has a concept of scope via the context (i.e. this
) and allows programmers to inject that context on a function using call
or apply
.
Are there any differences between Angular's, $scope
, and the keyword this
?
If there is a difference, then what is the value of this
within a controller or directive?
Thanks in advance :)
Upvotes: 2
Views: 693
Reputation: 5753
Yes, they are not the same at all. The constructor is just an instantiated new ed constructor (the function you wrote) created by the injector.
$scope
is more conceptually related to the DOM. In that elements with ng-controller
get that $scope
and child elements do as well. If a child element with its own scope (controller/directive) had the same properties as the parent scope You wouldn't be able to access them. It also has all of the internal information angular uses in its digest loop (dirty checking/ data binding) like watches,events,etc. I'd have a read through this
As for the myCtrl as
syntax, this is nice but all it really does is put the controller instance onto the scope. With the name that you set.
eg myCtrl as foo
is basically $scope.foo = myCtrlInstance;
. Which you are capable of doing in your controller as well.
Upvotes: 1