Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using Typescript with AngularJs and using the pattern where you:

$scope.vm = this;

and then just add your properties and method on your class. However it occurred to me that when I need to access a service I make it a property on my class which then adds it to my scope, which makes my scope potentially very large. My understanding is we want to keep the controller scope small. Since angular uses dirty checking doesn't this mean that any time anything changes on any services I have the whole scope (controller class) will be reprocessed?

So my question is, am I off base and this is not a big deal?

If it is a problem how have others addressed it?

Thanks

share|improve this question

2 Answers 2

up vote 0 down vote accepted

understanding is we want to keep the controller scope small. Since angular uses dirty checking doesn't this mean that any time anything changes on any services I have the whole scope (controller class) will be reprocessed?

No. Angular only dIffs what you bind to or watch. Nothing is watching stuff on the service.

share|improve this answer
    
Hmm that contradicts how dirty checking was explained to me, what your saying sounds like an observable, only what you bind is observed . . . I'm confused, or is it like everything on the scope that is bound or watched is reevaluated each time, and an observable system like knockout only does the one observable? So I just need to not bind too many things, does that sounds right? –  Ben Rethmeier Mar 17 at 21:19
    
Only what is bound is dirty checked. This is different from "only what is bound is observed". Also Angular by default doesn't do deep checks into the scope members –  basarat Mar 17 at 21:52
    
To be clear I don't need to worry too much about the scope getting large but more so the number of things I bind (or watch) agreed? –  Ben Rethmeier Mar 19 at 15:37
    
Yes. Exactly. Nailed it. –  basarat Mar 19 at 21:29

Try declaring your controllers as a class and use this and controllerAs.

The $scope.vm pattern is useful only because this may bind to different things in Javascript, especially when a "class" is declared entirely in the constructor function. This is normally not a problem when you use the controllerAs variable to call a Typescript class's method as this is very well defined.

Edit:

Style Y031 and Y032 from the angular style guide explains why vm is necessary, i.e. this confusion.

You do not need it in TypeScript because the semantics of this is very clear and in situations where you need to pass around function references, you can use lambdas ((...) => ...) to capture this.

share|improve this answer
    
Hmm my controller is a class . . not sure I understand what you are suggesting, can you point me to an example? Code. –  Ben Rethmeier Mar 19 at 1:33
    
I don't see how this solution prevents my services from being added to the scope, short of explicitly adding what I do and do not want on the scope, of course I can do that, but I don't wanna! ;) It sounds like what I need to pay attention to is the number of things I bind, and don't need to worry about the scope being large anyway. –  Ben Rethmeier Mar 19 at 15:36
    
My view can be summarized as avoid $scope as much as possible. controllerAs and the vm pattern are not there to prevent you from accessing $scope, but to make your life easier without it. (Angular authors have apologized for inventing it and it won't exist in 2.0, so it would be a good idea to avoid it.) –  billc.cn Mar 19 at 15:55
    
Yes I agree and will use the controller as, way going forward, scope is lamesville –  Ben Rethmeier Mar 19 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.