-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ng-scope class is set on root DOM element linked with public linking function (returned by $compile()) even if directive does not create new scope #1633
Comments
@danilsomsikov this works as designed. In fact a directive can create 2 sibling scopes as explained in http://docs.angularjs.org/guide/directive (check the "Understanding Transclusion and Scopes" section). BTW: there was a nice article published just few days back that also touches on problem of transclusion: |
Is this not because the transclusion itself creates a new scope? On 30 November 2012 15:48, Danil Somsikov notifications@github.com wrote:
|
But as you can see in fiddle there's only one scope created, however there're two elements having css class ng-scope. |
@danilsomsikov oh, sorry, didn't notice that both scopes got the same ID, I was sure that we are talking about this case: http://jsfiddle.net/GqqmS/4/ Anyway, I saw already an issue opened for those spans: #1293 Comments from Misko were:
So I guess it is the best to comment on your case in #1293 as this one seems to be duplicate of #1293. |
Spans are not the case as well. I don't know why did I put it to the sample code. |
@danilsomsikov Danil, thnx for providing more info, this starts to look interesting now. So this is not a new transcusion scope nor additional spans and I can't explain why this scope class is added without digging more into the code. Re-opening this one, probably people more familiar with $compile should look into it. |
The class is added at this line. |
|
Yes, however it is not the case in the issue I report. |
Yes you are right! So at https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L585 you can see that the transclude function is created by a call to compile(). It is this call to compile that applies the ng-scope. Now I guess the assumption is that if you are transcluding then you are going to create a new child scope for the transclusion, i.e. scope.$new(...). You are not doing this. You are just passing the directive's original scope into the transclusion function. But then you are not really getting much use out of transclude in this case - you could just let the compiler pass down to the child elements itself. I guess that you real use case is doing something more clever. But if it is then you should probably be creating a new scope and then being responsible for destroying it later, if necessary. |
It turns that public linking function (the one returned by $compile(...)) is setting ng-scope CSS class and $scope data even if element does not create new scope. |
As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months. Please try the newest versions of Angular ( Thanks! |
The issue hasn't been fixed in either version. |
I think I am experiencing a similar issue Fiddle: http://jsfiddle.net/U5UxX/ |
Even slimmer version of example against "snapshot": http://jsfiddle.net/zPqPR/ |
The problem is that $compile isn't to know whether you passed in a new scope or reused one from elsewhere. It currently assumes that one would never call it with a reused scope. |
If $compile doesn't know what kind of scope it gets, why would it set css class? I don't have particular use case, I found it when I was reading source code to study how compilation works. |
See http://jsfiddle.net/danilsomsikov/aEpkh/
Original fiddle with transclusion function http://jsfiddle.net/danilsomsikov/mMWyU/4/
The text was updated successfully, but these errors were encountered: