angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

An isolated-scoped-directive's ng-transclude loses parent scope when inside of an ng-repeat.

Open nbauernfeind opened this issue 13 years ago • 35 comments

The fiddle: http://jsfiddle.net/nCfBA/

To reproduce in short: 1) transcluded element needs to reference something in its parent scope, 2) directive's template needs to transclude inside of an ng-repeat. The transcluded element cannot read its original parent scope.

If included outside of the ng-repeat it can read the value in the parent scope (as one would expect).

I posted this to a google group (https://groups.google.com/forum/#!topic/angular/az8_uNV7KyE) but was directed to post it as a possible bug.

I don't mind trying to tackle and fix this, but I really have no idea where to start looking.

nbauernfeind avatar Jan 14 '13 21:01 nbauernfeind

This issue still exists in 1.0.5. Updated fiddle: http://jsfiddle.net/nCfBA/1/

I'd be happy to fix this, but don't know where to begin.

nbauernfeind avatar Mar 04 '13 21:03 nbauernfeind

+1

unscene avatar Apr 11 '13 21:04 unscene

+1 I have spend two days to find out that the problem was a combination of transclusion, isolated scope and ng-repeat :-(

MarcoPil avatar Apr 25 '13 11:04 MarcoPil

I'm still happy to try and fix this =).

nbauernfeind avatar Apr 25 '13 16:04 nbauernfeind

I am facing exactly the same issue. I think the problem is the $transclude function that keeps the reference of outer scope. I have created a new directive (replacement for ng-transclude) that uses correct scope.

angular.module("my").directive('myTransclude', function() {
    return {
        compile: function(tElement, tAttrs, transclude) {
            return function(scope, iElement, iAttrs) {
                transclude(scope.$new(), function(clone) {
                    iElement.append(clone);
                });
            };
        }
    };
});

However, I am not sure whether this is the correct solution but at least it's working fine for our use cases.

cristatus avatar Jun 03 '13 12:06 cristatus

Wow, thanks cristatus. That fixed my problem also where I had two directives on an element and one of those directives (vertical-center) had a transclude that was screwing up the other element's access to a parent scope. The directive with the transclude is shown here. Something pretty simple that enables dynamic blocks to be vertically centered within other blocks. Switching ng-transclude to my-transclude did the trick.

directive( 'verticalCenter', function () {
    return {
        restrict : 'AC',
        template : ' \
            <div style="display: table; width: 100%; height: 100%"> \
                <div style="display: table-cell; vertical-align:middle;" my-transclude> \
                </div>\
            </div>',
        transclude : true,
        scope: false
    }
});

oravecz avatar Jun 05 '13 22:06 oravecz

+1

paynen avatar Jun 19 '13 12:06 paynen

Looks like it's fixed in master 45f9f62367221b2aa097ba1d87d744e50140ddc7

cristatus avatar Jul 27 '13 23:07 cristatus

Not sure if it is: http://jsfiddle.net/33jQz/

petebacondarwin avatar Jul 28 '13 20:07 petebacondarwin

@petebacondarwin, I'm also a "victim" of this. Do you know of any quick fix for this?

gustavohenke avatar Sep 11 '13 20:09 gustavohenke

@gustavohenke Did you try the quick fix from @cristatus?

nbauernfeind avatar Oct 17 '13 16:10 nbauernfeind

+1

rrimer avatar Feb 05 '14 08:02 rrimer

it's still there.

ng-transclude with ng-if inside of directive with transclude: true & isolate scope.

cztomsik avatar Feb 21 '14 07:02 cztomsik

I was able to recreate this with ng-if:

http://jsfiddle.net/WqD7E/2/

and I can confirm that this line (https://github.com/angular/angular.js/blob/master/src/ng/directive/ngIf.js#L93) within ng-if is calling scope.$new() on a child scope of my directive, and not of the parent scope as you would expect. I bet that's what's happening within ngRepeat as well.

Here is how I overcame this. It sucks, but it works:

.directive('alteredTransclude', function() {
  return {
    compile: function(tElem, tAttrs, transclude) {
      return function(scope, elem, attrs) {
        var newScope = scope.$parent.$parent.$new(); // Call $parent to get to the scope you want
        transclude(newScope, function(clone) {
          elem.append(clone);
        });
      };
    }
  };
})

YourDeveloperFriend avatar Mar 04 '14 00:03 YourDeveloperFriend

+1

ivan-saorin avatar Mar 07 '14 22:03 ivan-saorin

Plunked a workaround for this, description can be found on stackoverflow.

McNull avatar Mar 17 '14 10:03 McNull

+1

bvaughn avatar May 02 '14 17:05 bvaughn

I have a fix for this at #7499

petebacondarwin avatar May 17 '14 17:05 petebacondarwin

+1

endorama avatar May 23 '14 16:05 endorama

@petebacondarwin how would you go about making the repeated item also accesible inside the transcluded scope?

This would allow writing custom "repeat" directives effortlessly (delegating to ng-repeat).

http://jsfiddle.net/xU5Cf

Thanks!

gabrielmaldi avatar Jun 18 '14 06:06 gabrielmaldi