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

Angular 1.0.3 breaks directives using ng-transclude

Open Addeventure opened this issue 14 years ago • 22 comments

ng-translude for directives is broken in angular 1.0.3. It seems to be related with the fix for issue #1313

Changing view using $location from a view that uses a directive with transcluded content throws: TypeError: Cannot read property '$$childHead' of null

Take a look at this simple snippet: http://jsfiddle.net/JJt3u/

Snippet fails in Chrome 23.0.1271.64, and Firefox 17.0

Addeventure avatar Nov 29 '12 10:11 Addeventure

This issue is not caused by that change, since the changed line does not get called before the exception is thrown.

The compiler is creating an extra scope (005 on my machine) that is not attached to a parent, it is the destruction of this that is failing. This extra scope is the one that is created if your directive template has more than one top level DOM node. (In the case of your fiddle it is the sneaky bit of whitespace between the div and the button.)

Here is a Plunker http://plnkr.co/edit/aSN1H4?p=preview. If you change which template is being used you will get different success or exception.

petebacondarwin avatar Nov 29 '12 11:11 petebacondarwin

Possibly due to this commit? https://github.com/angular/angular.js/commit/4e45a2f8e2149c38eaed8b9855873c165f8c5361#L0L709

petebacondarwin avatar Nov 29 '12 11:11 petebacondarwin

Ouch, yes that was a sneaky whitespace. Thank you for looking in to this! Just to clarify, it is not the directive template but the transcluded DOM which have more than one top node.

I'm surprised that I am the only one that have reported this issue (although 1.0.3 was released quite recently) as it feels like a very normal use case of directives.

One more clarification/question: Having more than one top node in transcluded dom is supposed to be supported right? (Otherwise I have a lot of templates to rewrite ;))

Addeventure avatar Nov 29 '12 13:11 Addeventure

Yes, sorry, top level nodes in the transcluded DOM. Multi top level nodes are supported - AngularJS just wraps them in a to make a single top level node.

On 29 November 2012 13:31, Andreas Duchen [email protected] wrote:

Ouch, yes that was a sneaky whitespace. Thank you for looking in to this! Just to clarify, it is not the directive template but the transcluded DOM which have more than one top node.

I'm surprised that I am the only one that have reported this issue (although 1.0.3 was released quite recently) as it feels like a very normal use case of directives.

One more clarification/question: Having more than one top node in transcluded dom is supposed to be supported right? (Otherwise I have a lot of templates to rewrite ;))

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1627#issuecomment-10847094.

petebacondarwin avatar Nov 29 '12 13:11 petebacondarwin

I'm having this issue as well.

qualidafial avatar Nov 29 '12 16:11 qualidafial

Fast solution commenting lines 7808 and 7809

Comment this: this.$parent = this.$$nextSibling = this.$$prevSibling = this.$$childHead = this.$$childTail = null;

Add this: this.$$nextSibling = null;

murilobr avatar Nov 29 '12 17:11 murilobr

Happens also without jQuery: http://jsfiddle.net/JJt3u/3/

pkozlowski-opensource avatar Nov 29 '12 18:11 pkozlowski-opensource

Also broken in 1.1.1

/edited from 1.1.0 (thanks @pkozlowski-opensource)

ggoodman avatar Nov 29 '12 19:11 ggoodman

@ggoodman 1.1.0 or 1.1.1?

pkozlowski-opensource avatar Nov 29 '12 19:11 pkozlowski-opensource

@murilobr - You are right - commenting out those lines does fix the problem: http://plnkr.co/edit/IOeW5Q?p=preview

petebacondarwin avatar Nov 29 '12 20:11 petebacondarwin

I'm seeing this too.

From what I can tell, it looks like every top level element in transcluded content (including whitespace) is being assigned the same scope (stored in data()).

This causes $destroy to be fired more than once on the scope when the elements are removed.

It's possible that this is an existing bug with $compile that just surfaced because of the change to null out $parent on $destroy.

dbinit avatar Nov 29 '12 20:11 dbinit

Changing that line from:

this.$parent = this.$$nextSibling = this.$$prevSibling = this.$$childHead =
this.$$childTail = null;

to:

this.$$nextSibling = this.$$prevSibling = this.$$childHead = this.$$childTail = null;

Also seems to fix the problem.

Or, perhaps a better solution, you can add:

if (!parent) return;

right below this in that same function:

var parent = this.$parent;

dbinit avatar Nov 29 '12 21:11 dbinit

One for Igor, I think!

petebacondarwin avatar Nov 29 '12 21:11 petebacondarwin

ok, I'm on this. The problem is due to bd524fc4e5fc0feffe85632a7a6560da6bd9b762 which surfaced a bug in destruction of a multi-rooted DOM with a single scope associated with all of the roots. we mistakinly destroy the shared scope onces for each root, which is wrong. once a scope is destroyed, it's destroyed. I'll work on a fix for this.

IgorMinar avatar Nov 29 '12 23:11 IgorMinar

Thanks for the fixes in here. You guys are savers of lives, or the time wherein one invests it.

leefernandes avatar Jan 06 '13 00:01 leefernandes

Is the fix rolled into a release? I am seeing this issue in conjunction with angular-ui and Angular 1.0.3 (from CDN). It appears after transitioning from a partial using the accordian directive.

I wrote it up on SO: http://stackoverflow.com/questions/14285932/angular-js-bootstrap-accordian-example#comment19838051_14285932

robertjchristian avatar Jan 11 '13 21:01 robertjchristian

@robertjchristian it was fixed in master but not yet released... What you can do is to take the snapshot of the bug-fix release (to be 1.0.4) from http://code.angularjs.org/snapshot/. This one should be rather stable (it has only bug and documentation fixes)

pkozlowski-opensource avatar Jan 11 '13 21:01 pkozlowski-opensource

That did the trick.  Thank you.Pawel Kozlowski [email protected] wrote:@robertjchristian it was fixed in master but not yet released... What you can do is to take the snapshot of the bug-fix release (to be 1.0.4) from http://code.angularjs.org/snapshot/. This one should be rather stable (it has only bug and documentation fixes)

— Reply to this email directly or view it on GitHub.

robertjchristian avatar Jan 11 '13 21:01 robertjchristian

Sorry to drudge up an old issue but would really appreciate if anyone has some insight. I'm trying to update an app from v1.0.2 to v1.0.6 and am pretty sure this change is causing an issue I'm seeing. Basically an NPE gets thrown when destroying a scope upon receiving an event broadcast. I think this happens because the broadcast tries to execute again on a scope that no longer exists and the broadcast can't be cancelled.

The error is something like this in Chrome:

TypeError: Cannot read property '$$nextSibling' of null
    at Object.Scope.$broadcast (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:8271:57)
    at Object.$scope.broadcastDone (http://run.plnkr.co/t0qjPOtC0B8TnSFG/app.js:7:12)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:6315:19
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:12903:13
    at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:8011:28)
    at Object.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:8091:23)
    at HTMLAnchorElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:12902:17)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:1972:10
    at Array.forEach (native)
    at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js:131:11) angular.js:5704

Here's a basic plunkr that demonstrates the problem(check the console to see the error, click the 'Done' link to trigger): http://plnkr.co/edit/zAUL4WliHpDp9c2K4xNo?p=preview

If you update the angular version to 1.0.2 in index.html it will work as it did for us previously, but starting with v1.0.3 we're see the error. It happens both in the latest versions of Chrome/Canary, Firefox and Safari so shouldn't be anything browser specific.

Not sure if this is the result of a bug or bad practice, if the later it would be awesome if someone could suggest an alternate implementation :-)

munsellj avatar Apr 25 '13 02:04 munsellj

:+1: even in 1.1.5

cm325 avatar Oct 25 '13 18:10 cm325