I am currently refactoring a large-ish angular app in an attempt to DRY up 
the code and slim down some very fat controllers. I have moved almost all 
my API access into services and it's working very well for me, but 
something has popped up which is really bothering me.

Essentially, I now have no use for scope inheritance in my controllers 
whatsoever. All my dependencies are injected, I never use anything from the 
parent scope. Furthermore, any data I want exposed to my views I explicitly 
attach to the scope for that controller, because I think it's clearer, more 
declarative, more testable etc. At the same time, I am aware that a lot of 
the time the scope will already contain that data because there are certain 
things I attach to the scope all over the place, e.g. currentUser. I would 
rather that the scope was not inherited from the enclosing scope, because 
it's simpler to test & reason about my controllers if I know I'm getting a 
blank-slate scope every time.

Here's an illustration of what's bothering me:

http://plnkr.co/edit/4yJ8KYfcdazIkyHNQaFp?p=preview

As you can see, there are two controllers, one inside the other. Each has 
its dependencies injected and attaches them to the scope for use by the 
view. They're completely encapsulated. And it works fine. Except, look at 
the inner controller! I've misspelt the name of my scope property, so the 
view shouldn't show anything! But of course it does show something, because 
the currentUser scope property is inherited from the enclosing view. This 
bugs me.

I've actually tried to write an alternative version of ngController that 
uses an isolate scope, but I can't get it to work (for some reason angular 
doesn't attach the scope to the view).

My questions are:

1. Is there an easy way to get an isolate scope in my controllers?
2. Is there a complicated/clever way to get an isolate scope in my 
controllers?
3. Should I just replace all my controllers with directives? (I don't want 
to do this)
4. Should I just use the "Controller as" syntax? (I don't want to do this 
either)
5. Should I just forget about this and get on with my life?

Thanks for reading this long and waffly post.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to