Reputation: 8548
In my application, I'm using this
instead of $scope
to save variables and functions, and using controller alias in the HTML to access.
In this case, how to can I update my view, do something like $digest()
or $apply()
of $scope
?
Or is necessary inject $scope
to do this?
Upvotes: 0
Views: 79
Reputation: 9800
The controllerAs
way have nothing to do with $scope
in the controller. It's a pattern to avoid contact between $scope
and the template and also improve readability. However, even though you are using controllerAs
syntax, you can inject $scope
on your controller with no problems. That's what controllerAs
is about, use the $scope
for propper tasks such as $scope.$apply
, but not as a view model.
It's not a bad practice injecting the $scope
even though you're using controllerAs
. But it would be a bad practice if you use $scope
to work like a view model. Anyhow, even if you don't inject, $scope
will exists somehow in the controller internals, it's part of a controller. The aproach of controllerAs
is to separate the role of view model from the $scope
. In the end the view model become a part of the scope but it's isolated from the rest of $scope
's features.
Upvotes: 1
Reputation: 3207
@Lai32290 Good job adopting the controllerAs
convention! It saves on headaches and makes things much clearer with nested scopes! As for your question, you cannot avoid the use of $scope
for the purposes of calling $digest
or $apply
. Remember though that behind the scenes, angularjs still attaches your controllerAs
to the $scope
, so it's still there.
You'll need to use it when events occur outside of angular's lifecycle - such as with sockets or events from other external libaries.
http://www.codelord.net/2015/11/11/angular-controlleras-when-should-you-use-scope/
Upvotes: 1