|
| 1 | +@name ngIf |
| 2 | +@restrict A |
| 3 | +@multiElement |
| 4 | + |
| 5 | +@description |
| 6 | +The `ngIf` directive removes or recreates a portion of the DOM tree based on an |
| 7 | +{expression}. If the expression assigned to `ngIf` evaluates to a false |
| 8 | +value then the element is removed from the DOM, otherwise a clone of the |
| 9 | +element is reinserted into the DOM. |
| 10 | + |
| 11 | +`ngIf` differs from `ngShow` and `ngHide` in that `ngIf` completely removes and recreates the |
| 12 | +element in the DOM rather than changing its visibility via the `display` css property. A common |
| 13 | +case when this difference is significant is when using css selectors that rely on an element's |
| 14 | +position within the DOM, such as the `:first-child` or `:last-child` pseudo-classes. |
| 15 | + |
| 16 | +Note that when an element is removed using `ngIf` its scope is destroyed and a new scope |
| 17 | +is created when the element is restored. The scope created within `ngIf` inherits from |
| 18 | +its parent scope using |
| 19 | +[prototypal inheritance](https://github.com/angular/angular.js/wiki/Understanding-Scopes#javascript-prototypal-inheritance). |
| 20 | +An important implication of this is if `ngModel` is used within `ngIf` to bind to |
| 21 | +a javascript primitive defined in the parent scope. In this case any modifications made to the |
| 22 | +variable within the child scope will override (hide) the value in the parent scope. |
| 23 | + |
| 24 | +Also, `ngIf` recreates elements using their compiled state. An example of this behavior |
| 25 | +is if an element's class attribute is directly modified after it's compiled, using something like |
| 26 | +jQuery's `.addClass()` method, and the element is later removed. When `ngIf` recreates the element |
| 27 | +the added class will be lost because the original compiled state is used to regenerate the element. |
| 28 | + |
| 29 | +Additionally, you can provide animations via the `ngAnimate` module to animate the `enter` |
| 30 | +and `leave` effects. |
| 31 | + |
| 32 | +@animations |
| 33 | +| Animation | Occurs | |
| 34 | +|----------------------------------|-------------------------------------| |
| 35 | +| {@link ng.$animate#enter enter} | just after the `ngIf` contents change and a new DOM element is created and injected into the `ngIf` container | |
| 36 | +| {@link ng.$animate#leave leave} | just before the `ngIf` contents are removed from the DOM | |
| 37 | + |
| 38 | +- @element ANY |
| 39 | + @scope |
| 40 | + @priority 600 |
| 41 | + @param {string} ngIf If the {@link guide/expression expression} is falsy then |
| 42 | + the element is removed from the DOM tree. If it is truthy a copy of the compiled |
| 43 | + element is added to the DOM tree. |
| 44 | + |
| 45 | +- @example |
| 46 | + <example module="ngAnimate" deps="angular-animate.js" animations="true" name="ng-if"> |
| 47 | + <file name="index.html"> |
| 48 | + <label>Click me: <input type="checkbox" ng-model="checked" ng-init="checked=true" /></label><br/> |
| 49 | + Show when checked: |
| 50 | + <span ng-if="checked" class="animate-if"> |
| 51 | + This is removed when the checkbox is unchecked. |
| 52 | + </span> |
| 53 | + </file> |
| 54 | + <file name="animations.css"> |
| 55 | + .animate-if { |
| 56 | + background:white; |
| 57 | + border:1px solid black; |
| 58 | + padding:10px; |
| 59 | + } |
| 60 | + |
| 61 | + .animate-if.ng-enter, .animate-if.ng-leave { |
| 62 | + transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s; |
| 63 | + } |
| 64 | + |
| 65 | + .animate-if.ng-enter, |
| 66 | + .animate-if.ng-leave.ng-leave-active { |
| 67 | + opacity:0; |
| 68 | + } |
| 69 | + |
| 70 | + .animate-if.ng-leave, |
| 71 | + .animate-if.ng-enter.ng-enter-active { |
| 72 | + opacity:1; |
| 73 | + } |
| 74 | + |
| 75 | + </file> |
| 76 | + |
| 77 | + </example> |
0 commit comments