Lai32290 Lai32290 - 14 days ago 4x
AngularJS Question

How to $apply() if not using $scope in Angular?

In my application, I'm using

instead of
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

Or is necessary inject
to do this?


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.