Answer the question
In order to leave comments, you need to log in
When to use $scope and when to use ControllerAs?
I am new to AngularJS and now, while adding another controller, I caught myself thinking that I had not used $scope for a long time, constantly resorting to the construction
angular.module('app.module')
.controller('SomeController', function() {
var vm = this;
});
Answer the question
In order to leave comments, you need to log in
Controller As is always desirable, and whether or not to inject into the $scope controller depends on whether you need to:
- subscribe to events
- watchers in controllers
The case when you really want to set something from the controller that should be inherited in all scopes, I deliberately omit because it shouldn't be done. If you really want, this can be done in the run module via $rootScope. But not from the controller, they should be isolated if possible and should not depend on the hierarchy.
If you manage to do without it and all this is taken care of by directives and services - nice, then you are on the right track ... most likely.
The only thing is... why do you write SomeController as vm... In theory, you should write a controller alias there so that it would be clear which methods are twitching. It is clear in the sense of not angular but you.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question