ngIf with replace:true on directive template
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!
@thetrevdev - glad you have got something working for you. If it turns out to be very helpful perhaps you could submit a patch?
I think that this may be fixed by #12975
I think I'm still seeing this issue on v1.4.8. My directive had replace: true and an ng-if on the root element. This let the directive be rendered but none of the children elements had the correct scope. Removing replace: true fixed the issue.
Also still experiencing this issue. I will look at avoiding replace: true, but it's going to be a bit of a pain.
@adambuczynski - other than CSS that is too constraining, are there any other reason why this is a pain?
@petebacondarwin Yeah, CSS was one of the initial pains I ran into, but it was late at night and today's a new day, so it might turn out not as bad as I originally thought! :)
There are a few other reasons I identified yesterday thought, one is that we need to apply an ng-if condition on the parent element of the directive. Currently, we are using ng-show for that, but the drawback of that is, that the scope/controller is not re-initialized when the directive is shown. So for that reason we wanted to move to using ng-if (which led me to this issue in the first place).
In order to not have to put the ng-if logic in our main templates, we embed it in the directive itself, which judging from the comments in this thread, seems to be a common use case. However, if the directive is not replacing the parent element, it will not work as expected, as it will only remove the child element, and not the parent element. Moving the ng-if's to the main template is not an option.
Second, to avoid repetition we also apply some classes to the parent element via the directive (via attributes on the directive's template). These get merged nicely now with any classes that are applied to the directive element in the main template. With replace: false this will no longer work, and we'll be forced to apply the classes programmatically via the link function, which isn't the end of the world, but just another (somewhat redundant) step that we have to take.
Anyway, I only found out that replace: true is deprecated by stumbling upon this issue. Most, if not all of our directives use replace: true, so I will run by all of them today and see if any other issues arise if we set it to false. It would have been good to receive deprecated notices in the console though, to alert developers of the fact they are using a deprecated API.
Will report back after I've had another good look at it today, and see if I can work around them while not using replace: true. If any other pain points come up I will let you know.
@adambuczynski - be aware that though replace:true is deprecated, we will not remove it from Angular 1.x - it is already gone from Angular 2. What it does mean is that we are not going to spend much time trying to fix the many complex and difficult corner case problems it has.
Thanks, yes I got that form reading more about it. I do intend to migrate to Angular 2 sooner rather than later, so figuring out now how to best use non-replacing directives would be the right step to take.
To be honest though, I don't think wanting to apply ng-if on the parent element via the directive template qualifies as a corner case. It seems like a fairly common use case.
It may not be "corner" but for sure it is "difficult". I haven't seen any PRs offering to fix it :-)
@petebacondarwin Well, most directives I was able to convert painlessly, but there were the couple where some CSS tweaks were necessary. As for the one with the ng-if on it, I've managed to refactor it in a way that the ng-if can still be applied on the inner element, and with some CSS tweaks it seems to be looking and working the same now. So didn't need replace: true after all.
Do Angular 2 components offer a way to apply directives and other attributes on to the parent element via the directive templates? Because that currently seems to be the biggest drawback in Angular 1 directives, when not using replace: true.
It took me awhile to figure out I had this issue. I spent a fair amount of time assuming I had some sort of error in my scope configuration. It was only on a whim that I removed ng-if from the root element and discovered this to be the problem. It would be good to have some sort of warning.
Just spent several hours trying to figure out what was wrong with my code to finally narrow it down to the ng-if. Happy there is a known issue...