Is there a way to tell AngularJS not to display the top HTML element which has ng-if directive. I want angular to display child content only.
Angular Code:
<div ng-if="checked" id="div1"> <div id="div2">ABC</div> <div id="div3">KLM</div> <div id="div4">PQR</div> </div>
Rendered HTML:
<div id="div1"> <div id="div2">ABC</div> <div id="div3">KLM</div> <div id="div4">PQR</div> </div>
What I want:
<div id="div2">ABC</div> <div id="div3">KLM</div> <div id="div4">PQR</div>
Here is a fiddle. http://jsfiddle.net/9mgTS/. I do not want #div1
in HTML. I just want #div2,3,4
if checked
is true
.
A possible solution can be adding ng-if to all child elements but I do not want to do this.
So if you can have choice between ngIf and hidden, prefer hidden.
The Angular ngIf directive works essentially as an if statement for HTML, adding this missing feature to the language under the form of the special ngIf attribute. In this example, the container div will only be shown to the user if the user is logged in, otherwise the whole content of the div is not visible.
The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.
There's a currently-undocumented pair of directives, ng-if-start
and ng-if-end
, that you can use for this. They behave analogously to the documented ng-repeat-start
and ng-repeat-end
directives, and you can see the unit tests for them if you like.
For example, given the following code:
<ul> <li ng-if-start="true">a</li> <li>b</li> <li>c</li> <li ng-if-end>d</li> <li ng-if-start="false">1</li> <li>2</li> <li>3</li> <li ng-if-end>4</li> </ul>
the first four li
s will be shown and the final four li
s will be hidden.
Here's a live example on CodePen: http://codepen.io/anon/pen/PqEJYV
There are also ng-show-start
and ng-show-end
directives that work exactly the way you would expect them to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With