Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Scope() on template of isolated directive is wrong #5704

Closed
yarontt opened this issue Jan 9, 2014 · 16 comments
Closed

Scope() on template of isolated directive is wrong #5704

yarontt opened this issue Jan 9, 2014 · 16 comments
Assignees

Comments

@yarontt
Copy link

yarontt commented Jan 9, 2014

I have a directive which has an isolated scope and a template.
Although the bindings in the template refer to the isolated scope as they should, scope() and data() do not show this scope.

For all purposes the scope of the template is indeed the isolated scope, so I'd expect scope() to return it (or at least to see it in data()).

This makes debugging a bit difficult.

See
http://jsfiddle.net/yarontt/S5bnE/

@caitp
Copy link
Contributor

caitp commented Jan 9, 2014

27e9340 (due to 909cabd) (You can see it in data with key "$isolateScope" or "$isolateScopeNoTemplate")

@yarontt
Copy link
Author

yarontt commented Jan 9, 2014

Ah... not really.
On the elements of the template there's no sign of the isolated scope.

But seriously... from the perspective of the template the scope IS the isolated scope. It's linked with it. So it just makes sense scope() would return that isolated scope. It's isolated from the point of view of the element on which the directive sits, but on the template of that directive it effectively becomes THE scope.

@caitp
Copy link
Contributor

caitp commented Jan 9, 2014

Well, that's not the way things work anymore, the isolate scopes are isolated similar to the way the shadow DOM is isolated from the light DOM (that might be a bit of a stretch, but the reasoning is similar)

@yarontt
Copy link
Author

yarontt commented Jan 9, 2014

So basically the template is linked against a scope no one can ever see???

That sounds kind of strange. The template is an integral part of the directive.

And for debugging purposes, how can I tell which scope an element was linked against? At the least, I'd expect the data() of the template elements to contain that scope. Though, as mentioned before, I see the template as an integral part of the directive. If the directive has an isolated scope then so does the template (it is linked with it).

@caitp
Copy link
Contributor

caitp commented Jan 9, 2014

You can see it with the isolateScope() method, looking at the root element of the directive, for debugging purposes.

@yarontt
Copy link
Author

yarontt commented Jan 9, 2014

Yeah... true.
That feels kind of off. I need to always keep in mind what's the root element of a directive, and this makes writing any automatic debugging tools nearly impossible.

I totally understand the separation of isolated scope people are aiming for, I feel that in this specific case this has gone a bit too far to the point where it interferes and makes life harder instead of easier.

@ghost ghost assigned tbosch Jan 10, 2014
@tbosch
Copy link
Contributor

tbosch commented Jan 10, 2014

Sorry, but this works as intended. Closing this.

@tbosch tbosch closed this as completed Jan 10, 2014
@yarontt
Copy link
Author

yarontt commented Jan 10, 2014

Assuming this does work as intended, is there any way to know given an element of a template that it is indeed part of a template?
If I want to write some automatic tool for debugging, I need to be able to know which scopes apply to which elements.

@tbosch
Copy link
Contributor

tbosch commented Jan 11, 2014

You can check the properties $scope, $isolateScope and isolateScopeNoTemplate that are stored on element.data(key) in the parents of an element. Just look into the implementation of element.scope and element.isolateScope: https://github.com/angular/angular.js/blob/master/src/jqLite.js#L438

@yarontt
Copy link
Author

yarontt commented Jan 11, 2014

You mean traverse the tree up until isolatedScope() returns something?

I have to admit that to me it seems really strange.
Just so I understand why you all think this is working as intended, could you explain what scope() is for and what's the new philosophy about isolated scopes?

@shahata
Copy link
Contributor

shahata commented Apr 1, 2014

@caitp @tbosch I think I disagree that this is the intended behavior. My interpretation of 27e9340, which I read 3 times before saying that I think you are wrong :) -

Invoking scope() on child elements of some node with an isolated scope, given that there is no other scope in the middle, should not return the isolated scope only if that directive does not have a template. Which completely makes sense. Hence the name $isolateScopeNoTemplate.

The scope() function should return the scope that is used for evaluating expressions on the element you are querying. You can clearly see in the implementation of scope() that it looks for both $scope and $isolateScope in the inherited data if it did not find a $scope on the current element.

The only edge case is when you are querying an element which is the root of an isolated scope, where one directive uses the isolated scope but all other directives use the parent scope (or a new scope created by {scope:true}). The "right" thing to return in scope() in this case is indeed the parent scope and the purpose of isolateScope() is to be able to also get the scope used by the single directive which got the isolated scope.

This works just as I described for directives that have a templateUrl, there just seems to be a minor bug with directives that load the template synchronously using template. I've fixed this issue in #6942

@shahata
Copy link
Contributor

shahata commented Apr 4, 2014

@caitp @tbosch do you have any input on this?

@caitp
Copy link
Contributor

caitp commented Apr 4, 2014

My input is that we probably should fix jqLite#scope to not also search for $isolateScope. No part of angular core depends on this, and it kind of breaks the whole "isolation" thing.

@shahata
Copy link
Contributor

shahata commented Apr 4, 2014

If this is what should be done or what was intended in this commit, then what is the point of separating $isolateScope and $isolateScopeNoTemplate if they are treated exactly the same? Do you say that separating them was also a mistake?

@caitp
Copy link
Contributor

caitp commented Apr 4, 2014

it may have been necessary to separate them, because of the difference between sync and async compilation. But I'm not positive. I can't really think of a good higher-level reason for the separation

@shahata
Copy link
Contributor

shahata commented Apr 4, 2014

Maybe we should ask @IgorMinar for his opinion on this and his actual intentions in his commit (27e9340)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants