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

fix(ngIf): ngIf removes elements dynamically added to it #4726

Closed
wants to merge 3 commits into from

Conversation

btford
Copy link
Contributor

@btford btford commented Oct 30, 2013

When using ngIf with ngInclude on the same element, ngIf previously did not remove elements added by ngInclude. Similarly, when using ngIfStart/End, ngIf will miss elements added between the start/end markers added after ngIf is linked.

This commit changes the behavior of ngIf to add a comment node at the end of its elements such that elements between the starting comment and this ending comment are removed when ngIf's predicate does not hold.

@btford
Copy link
Contributor Author

btford commented Oct 30, 2013

@IgorMinar reviewed most of this, so I think it should be good to merge.

When using ngIf with ngInclude on the same element, ngIf previously did not remove
elements added by ngInclude. Similarly, when using ngIfStart/End, ngIf will miss
elements added between the start/end markers added after ngIf is linked.

This commit changes the behavior of ngIf to add a comment node at the end of its
elements such that elements between the starting comment and this ending comment
are removed when ngIf's predicate does not hold.
@btford
Copy link
Contributor Author

btford commented Oct 30, 2013

Merged.

@MrIsaacs
Copy link

MrIsaacs commented Nov 5, 2013

so this means that we can use ng-if with ng-include again?
is it just a workaround iin the compiler until the stable release is out?

@beninfobelt
Copy link

Updating to #1.2.1 worked for me.

Also, for those trouble shooting, it is important to know that ng-include requires the url path to be from the app root directory and not from the same directory where the partial.html lives. (whereas partial.html is the view file that the inline ng-include markup tag can be found).

For example:

Correct:
div ng-include src=" '/views/partials/tabSlides/add-more.html' ">

Incorrect
div ng-include src=" 'add-more.html' ">

plnkr === http://mle.mymiddleearth.com/files/2013/07/aint-nobody-got-time-for-that.png

@wardbell
Copy link

It's working for me now ... with a glitch; must wrap in a <div>. See the following:

 <div> <!-- breaks w/o this div :( -->
      <div data-ng-if="tab.oneChoice"  data-ng-include="'app/views/orderItemOptionRadio.html'"></div>
      <div data-ng-if="!tab.oneChoice" data-ng-include="'app/views/orderItemOptionCheckbox.html'"></div>
</div>

Remove the outer div and you get TypeError: Cannot call method 'insertBefore' of null.

Not a big deal but it was a mystery to me what I was doing wrong until I tried an outer <div> for no particular reason.

@ariesjia
Copy link

Awesome, i can use ng-include and ng-if together~~~

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants