Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

ng-If doesn't work with ng-Include #3307

Closed
wardbell opened this issue Jul 23, 2013 · 9 comments
Closed

ng-If doesn't work with ng-Include #3307

wardbell opened this issue Jul 23, 2013 · 9 comments
Milestone

Comments

@wardbell
Copy link

This doesn't work ... although it seems it should:

<div data-ng-if="oneChoice"  data-ng-include="'optionsRadio.html'"></div>                         
<div data-ng-if="!oneChoice" data-ng-include="optionsCheckbox.html'"></div>

This alternative works as expected:

<div data-ng-switch="oneChoice">
    <div data-ng-switch-when="true"  data-ng-include="'optionsRadio.html'"></div>                         
    <div data-ng-switch-when="false" data-ng-include="'optionsCheckbox.html'"></div>
</div>
@lucipacurar
Copy link

<div data-ng-if="oneChoice">
<div data-ng-include="'optionsRadio.html'"></div>
</div>

Did you try this?

@wardbell
Copy link
Author

I haven't tried that. Thanks for the suggestion. But I must say, even if it worked, it would be less appetizing than ngSwitch. I really should be able to use ngIf in the same div, not in a wrapper div. It should be given the higher priority that ngSwitchWhen clearly has. In sum, that is a workaround, not a fix, and I think ngSwitch is the better workaround. Cheers.

@thenikso
Copy link
Contributor

I'm having the same problem in 1.2.0rc1. @lucassp solution doesn't work for me.

Here is a plunker: http://plnkr.co/MtRpmI

@andr2
Copy link

andr2 commented Aug 15, 2013

This is really annoying bug. Met it immediately after migration to 1.2.0rc.
Tried to fix it, but didn't realize. Rollbacking...

@jbourassa
Copy link

Same as @andr2: couldn't fix, rolling back 😢

@jchapron
Copy link

Same as @thenikso. It's also worth noting that activating ng-if multiple times will add the ng-include multiple times too, as you can see on this plunker : http://plnkr.co/BJUu5E

@pthrasher
Copy link

So, after the rc2 upgrade, it appears part of the problem is fixed. ng-if + ng-include now works, but you still get extra elements when they're on the same tag.

http://embed.plnkr.co/IZfOLU/preview

@vojtajina
Copy link
Contributor

<div ng-include="x" ng-if="y"></div> does not work, because we changed ng-include to use transclusion (aa2133a). The reason for this change was animations.

There are two workarounds for this issue:

  1. nesting ng-include into ng-if (<div ng-if="y"><div ng-include="x"></div></div>). This was broken (http://embed.plnkr.co/IZfOLU/preview), but it's now fixed in the master.
  2. do not use ng-if and rather hide it by setting the ng-include value to null (eg. scope.x = null;).

I agree, that as a user, I wanna be able to use ng-if together with ng-include as it is more expressive (workaround 1. is ugly, workaround 2. forces me to have another variable to store the old x value before setting it to null, so that I can bring it back). Hopefully we can solve this by teaching the compiler to allow multiple transclusions on the same element (#4217). This is however not trivial change and it's not gonna happen before 1.2.

There is another issue, that there is no error when a single element contain multiple transcluding directives. This issue will be fixed in 1.2 (see #4217).

I'm closing this issue and creating a new one for allowing multiple transcluding directives on the same element (#4357).

IgorMinar added a commit to IgorMinar/angular.js that referenced this issue Oct 11, 2013
Issue an error and abort compilation when two directives that ask for transclusion are found
on a single element. This configuration is not supported and we previously failed to issue
the error because in the case of element transclusion the compilation is re-started and this
caused the compilation context to be lost.

The ngRepeat directive has been special-cased to bypass this warning because it knows how to
handle this scenario internally.

This is not an ideal solution to the problem of multiple transclusions per element, we are
hoping to have this configuration supported by the compiler in the future. See angular#4357.

Closes angular#3893
Closes angular#4217
Closes angular#3307
IgorMinar added a commit to IgorMinar/angular.js that referenced this issue Oct 12, 2013
Issue an error and abort compilation when two directives that ask for transclusion are found
on a single element. This configuration is not supported and we previously failed to issue
the error because in the case of element transclusion the compilation is re-started and this
caused the compilation context to be lost.

The ngRepeat directive has been special-cased to bypass this warning because it knows how to
handle this scenario internally.

This is not an ideal solution to the problem of multiple transclusions per element, we are
hoping to have this configuration supported by the compiler in the future. See angular#4357.

Closes angular#3893
Closes angular#4217
Closes angular#3307
IgorMinar added a commit that referenced this issue Oct 12, 2013
Issue an error and abort compilation when two directives that ask for transclusion are found
on a single element. This configuration is not supported and we previously failed to issue
the error because in the case of element transclusion the compilation is re-started and this
caused the compilation context to be lost.

The ngRepeat directive has been special-cased to bypass this warning because it knows how to
handle this scenario internally.

This is not an ideal solution to the problem of multiple transclusions per element, we are
hoping to have this configuration supported by the compiler in the future. See #4357.

Closes #3893
Closes #4217
Closes #3307
@vojtajina
Copy link
Contributor

Ok, we decided to special case ng-if + ng-include to make this work, see #4726

Hopefully, we can solve this in a more general way in the future.

jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
Issue an error and abort compilation when two directives that ask for transclusion are found
on a single element. This configuration is not supported and we previously failed to issue
the error because in the case of element transclusion the compilation is re-started and this
caused the compilation context to be lost.

The ngRepeat directive has been special-cased to bypass this warning because it knows how to
handle this scenario internally.

This is not an ideal solution to the problem of multiple transclusions per element, we are
hoping to have this configuration supported by the compiler in the future. See angular#4357.

Closes angular#3893
Closes angular#4217
Closes angular#3307
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
Issue an error and abort compilation when two directives that ask for transclusion are found
on a single element. This configuration is not supported and we previously failed to issue
the error because in the case of element transclusion the compilation is re-started and this
caused the compilation context to be lost.

The ngRepeat directive has been special-cased to bypass this warning because it knows how to
handle this scenario internally.

This is not an ideal solution to the problem of multiple transclusions per element, we are
hoping to have this configuration supported by the compiler in the future. See angular#4357.

Closes angular#3893
Closes angular#4217
Closes angular#3307
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants