0

I can access controller variable on markup by using controller alias dot(.) variable name, so why we need $scope separately.. can we use the controller context and $scope interchangeably .. or there is some thing specific for which $scope is designed.

  • when you use the ControllerAs syntax, your controller becomes a property of `$scope`. `$scope` is always present for the two way binding, but is implicit in the HTML. – Claies Oct 05 '15 at 07:28
  • 6
    Search and you shall find http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers – masimplo Oct 05 '15 at 07:28
  • This is a common question. You need to understand MVC, and MVVM to know $scope is different from a controller. – Michael Kang Oct 05 '15 at 07:32
  • Duplicate! The accepted answer in the question referenced mxa055's comment is top quality. Seriously, just read that. – hally9k Oct 05 '15 at 10:03

2 Answers2

1

This is a good question, but perhaps too broad to get single clear answer. I'll offer my thoughts.

$scope was designed to be the view's model - the "VM" in MVVM pattern.

controller-as was introduced to follow an MVC pattern more closely where $scope inheritance is overkill and unnecessary. The "C" corresponds to an Angular controller, and allows the view to trigger a controller action directly.

I think the introduction of controller-as was a nice change. But I believe the right way to use it is for calling methods that is under the immediate control of the controller. In my opinion, I think that methods in controller scope should not propagate up the $scope stack, and call another method higher up the $scope chain. Doing so introduces complex dependencies, which is hard to understand and maintain. Controller-as prevents that.

Although controller-as can be used for storing models (not just for calling methods), I don't believe that it should. That is the job of the view model or $scope. I know others might feel differently.

Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • actually, as soon as you use the Controller-as syntax, your controller **becomes** a view model, represented by a property bound to `$scope`. unfortunately, this leads to people constantly using `as vm` everywhere, and creating a different kind of issue, but that's another discussion. – Claies Oct 05 '15 at 08:06
  • I'm not sure I agree with a "controller" becoming a view model. Although, that might be what's technically happening, I think its the wrong way to think about it. A "controller" is a "controller" - should never become a view model. I never really liked how its open for interpretation. – Michael Kang Oct 05 '15 at 08:11
  • true, it's a violation of the principles of MVVM, but it is what happens. Before the controller as was introduced, people used to do the same kind of thing by putting `$scope.vm = this` at the top of their controllers. Still see a lot of code written that way, too..... – Claies Oct 05 '15 at 08:13
  • honestly, using the Controller-As ends up feeling more strictly like an MVP (Model View Presenter) framework, since the "controller" and the "view model" are both collectively one object. Probably why so many people just lump these in as MV* frameworks. – Claies Oct 05 '15 at 08:18
0

$scope is not a singleton service. Meaning, when we inject it somewhere, usually it will create a new child scope. So, using $scope, we can propagate events for all its parents/children.

Also, we can store some "global" variables methods in parent scope.

If you use controller-as syntax, you are not able to work with this nesting. But code becomes more predictible.

Dee
  • 43
  • 6
  • actually, *you can* use nesting when using `controller-as`, what you can't do is use nesting when using `as vm` for every controller. – Claies Oct 05 '15 at 07:35