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

ngIf with replace:true on directive template

Open bertrandgressier opened this issue 12 years ago • 32 comments

Hello team,

We have a regression with the last angular version (since v1.3.0-beta.11)

In this commit https://github.com/angular/angular.js/commit/d71df9f83cd3882295ca01b1bb8ad7fb024165b6#diff-1c8fbee1402a2ef3c203def41d1960cdR97, you modified the child scope ngIf uses. Now with this modification, using ngIf on the root element of a directive template that is configured with replace:true, the $scope is not the expected one. (it's not the same)

We tried to build a reduced test case to show the regression :

  • 1.3.0-beta 10 : http://jsbin.com/sakuto/9/edit
  • 1.3.0-beta 11 : http://jsbin.com/yubetu/1/edit

As you can see, the first directive failed in beta 11.

What do you think ? Is it a bug or the new expected behaviour ? If so, maybe we should detail the documentation.

thx!

bertrandgressier avatar Oct 30 '14 10:10 bertrandgressier

Hi, @caitp could you help us on this one? ;-)

clakech avatar Oct 30 '14 10:10 clakech

since you asked nicely ^^

caitp avatar Oct 30 '14 11:10 caitp

so, you can work around this by not using an element transclusion directive as the root element of the template --- I'm not sure exactly why this worked in beta 10 though, doesn't make a lot of sense.

Lets dig through the changelog and see if it's listed in any of the breaking changes

caitp avatar Oct 30 '14 11:10 caitp

@petebacondarwin there are no breaking changes for $compile in beta 11, i think we forgot to add them

caitp avatar Oct 30 '14 11:10 caitp

I wonder if we could make this more palatable --- like, if you have element transclusion at the root of a replacement template, it should add the original parent scope in a custom data field, and use that when getting the parent scope.

caitp avatar Oct 30 '14 11:10 caitp

Thank you for your help about this! I knew your advice would be useful! ^^

We would definitely love to see a breaking change in the changelog and a new palatable behavior of angular in this use case.

We hesitate between removing replace:true and avoid using element transclusion directive as root element.

clakech avatar Oct 30 '14 13:10 clakech

Ok so I added another exemple in the jsbin with a use case where the ng-if is not on the root element of the directive:

http://jsbin.com/fahoz/1/edit?html,js,output

clakech avatar Oct 30 '14 13:10 clakech

seeing this as well, since upgrading to 1.3.1. my template is

template =  '<div class="tab-content" ng-if="visible">' +
                    '<div ng-if="tab" class="tab-inner-content clearfix" ng-include="tab"></div>' +
                   '</div>';

and it broke

pocesar avatar Nov 07 '14 02:11 pocesar

OK so someone need to propose a breaking change for this but where should we document this ? in ngIf doc ? in directive doc ? in changelog ?

clakech avatar Nov 07 '14 10:11 clakech

Only just seen this one. Let me take a look this week.

petebacondarwin avatar Nov 10 '14 14:11 petebacondarwin

I had this issue too. It seems to work if you wrap the root element (with the ngIf) in a new element (without ngIf) - as a temporary fix. http://plnkr.co/edit/9wgADGTILYINqHEdY1KE?p=preview

marlun78 avatar Nov 11 '14 14:11 marlun78

Moving to 1.3.4 as I suspect that this is going to be a tricky fix (and not something that can be knocked up and suitably reviewed by Monday).

petebacondarwin avatar Nov 15 '14 23:11 petebacondarwin

I have spent some time looking at this. I feel that it is not a bug in ngIf - ngIf is doing the right thing in asking for its new scope rather than guessing that it might be the parent scope.

Instead it is a more general bug regarding replace directives that have an element transclude directive at the root of their template.

This is a very hard bug to fix, since the original directive is being replaced by the element transclude directive; but that directive itself has been removed and replaced by a holder comment. So effectively we lose one layer of scope:

Normally without element transclude at the root you get:

{$scope-1}
<replace-isolated-transclude-directive>
  {$scope-2}
  <div>
    <!-- ng-if -->
      {$scope-2a}
      <ng-transclude>
        {$scope-1a}
      </ng-transclude>
  </div>
<replace-isolated-transclude-directive>

Here you can see that $scope-2 is the isolated scope of the directive, $scope-2a is the scope transcluded from $scope-2 of and $scope-1a is the transcluded scope from outside the transclusion directive.

If the ngIf is at the root of the template then $scope-2 never gets to the ngIf since by the time we are passing it through the link function, the ngIf has been compiled to a linkFn already bound to the outer $scope-1a:

{$scope-1}
<replace-isolated-transclude-directive>
  <!-- ng-if -->
    {$scope-1a}
    <ng-transclude>
      {$scope-??}
    </ng-transclude>
</replace-isolated-transclude-directive>

petebacondarwin avatar Dec 02 '14 13:12 petebacondarwin

This is one of the many reasons that we have deprecated replace as it creates so many of these hard to fix corner cases.

petebacondarwin avatar Dec 02 '14 13:12 petebacondarwin

@caitp's comment: https://github.com/angular/angular.js/issues/9837#issuecomment-61076800 is on the right track but the trick is how to do this in a non-hacky way that doesn't introduce more bugs elsewhere.

petebacondarwin avatar Dec 02 '14 13:12 petebacondarwin

Hi, has there been any progress on this, @petebacondarwin ?

matias-sandell avatar Mar 30 '15 12:03 matias-sandell

Directives that do replace cause so many problems. Given that we can almost always work around it with non-replace directives, I am going to move this to the Ice Box for now.

petebacondarwin avatar Jul 30 '15 16:07 petebacondarwin

Is there any way to detect this and throw a warning?

I understand all the reasons to not fix and to deprecate replace... But as far as migrating an existing project this is a breaking change that isn't documented. https://code.angularjs.org/1.3.18/docs/guide/migration#angular-html-compiler-compile- This refers to other more broken use cases I feel.

We are migrating large apps that happened to have used replace: true and its difficult to know where we might have used transclusion. If we could at least identify or warn we could go and fix the replace attribute. Right now trying to replace every single directive across many apps would be quite difficult.

thetrevdev avatar Sep 16 '15 01:09 thetrevdev

@thetrevdev - as a start you could do a search of your code base for directives that employ replace: true. Then look at their templates. If there is one of ng-if, ng-repeat or ng-switch on the root node of the template then you will need to fix it.

petebacondarwin avatar Sep 16 '15 12:09 petebacondarwin

Thats pretty difficult to do with a very large app.
I have currently made a local fork of angular that errors when it detects this case while migrating. When templates are involved and have isolate scope it marks the directives. I check here for the offending transclusion.

https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L2104

thetrevdev avatar Sep 16 '15 18:09 thetrevdev