Hi Aleck, thanks for commenting. You're quite right about needing proper
tests of course, but the inherited baggage still annoys me. And I think it
makes the tests less reliable, because the scope in the tests will be
subtly different from the scope in use - I would like to have *total*
control of my scope so I know that my tests and my app are both using the
controller in a precisely identical context.
Having said that, I'm definitely open to the possibility that it's more
bother than it's worth. At this point I really just want to know if it can
be done :)
On Thursday, 1 May 2014 06:58:15 UTC+1, Aleck Landgraf wrote:
>
> Hi Matthew,
>
> I agree with Sander and would add that some basic controller test coverage
> for your inner controller should verify that you misnamed a variable.
>
> function create_inner_controller(){
> inner_controller = controller('inner_controller', {
> $scope: inner_controller_scope,
> CurrentUser: 'test user'
> });
> }
> it("should have a proper scope variable name", function() {
> // arrange
> create_inner_controller();
>
> // act
> inner_controller_scope.$digest();
>
> // assertions
> expect(inner_controller_scope.currentUser).toBe('test user');
> });
>
> -Aleck
>
> On Wednesday, April 30, 2014 2:10:48 AM UTC-7, Matthew Simpson wrote:
>>
>> 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.