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?

Lai32290
  • 8,062
  • 19
  • 65
  • 99
  • Any particular reason you're avoiding `$scope`? It's used for data binding between the view and the controller. – Sterling Archer Oct 10 '16 at 18:04
  • Using `this` has nothing to do with saving variables and functions. Would be same number of variables if you used `$scope` – charlietfl Oct 10 '16 at 18:05
  • @SterlingArcher I'm not avoiding `$scope` specifically, just trying fallow the @john_papa style guide (https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#controllers) – Lai32290 Oct 10 '16 at 18:08
  • @SterlingArcher not necessarily if using `controllerAs` ... don't need to inject it for all controllers – charlietfl Oct 10 '16 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. – Heretic Monkey Oct 10 '16 at 18:08
  • @MikeMcCaughan I'm using `Q` to manage my promise (`Q.all()`), but when I setting the return value in `then` function, is not update my view, so I'm finding some solution for this. – Lai32290 Oct 10 '16 at 18:10
  • probably related: http://stackoverflow.com/a/30644370/2495283 – Claies Oct 10 '16 at 18:16
  • provide some code please – Merlin Oct 10 '16 at 18:40

2 Answers2

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.

lenilsondc
  • 9,590
  • 2
  • 25
  • 40
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/

Zach
  • 3,157
  • 1
  • 19
  • 32