0

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?

8
  • Any particular reason you're avoiding $scope? It's used for data binding between the view and the controller. Commented Oct 10, 2016 at 18:04
  • Using this has nothing to do with saving variables and functions. Would be same number of variables if you used $scope Commented Oct 10, 2016 at 18:05
  • @SterlingArcher I'm not avoiding $scope specifically, just trying fallow the @john_papa style guide (github.com/johnpapa/angular-styleguide/blob/master/a1/…) Commented Oct 10, 2016 at 18:08
  • @SterlingArcher not necessarily if using controllerAs ... don't need to inject it for all controllers Commented Oct 10, 2016 at 18:08
  • 2
    The question you should be asking yourself is, "why do I need to use $digest() or $apply()?" The need to use those should be very rare. Commented Oct 10, 2016 at 18:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So is not bad practice inject $scope just for update my view, in this case?
It's not a bad practice inject $scope even though you're using controllerAs. But it would be a bad practice if you use $scope to work like a view model.
Thanks you so so much!
That's right! You should adjust your question pointing to the real problem we discussed here.
1

@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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.