|
| 1 | +A special element that can hold structural directives without adding new elements to the DOM. |
| 2 | + |
| 3 | +The `<ng-container>` allows us to use structural directives without any extra element, making sure |
| 4 | +that the only DOM changes being applied are those dictated by the directives themselves. |
| 5 | + |
| 6 | +This not only increases performance \(even so slightly\) since the browser ends up rendering less |
| 7 | +elements but can also be a valuable asset in having cleaner DOMs and styles alike. |
| 8 | + |
| 9 | +It can for example enable us to use structural directives without breaking styling dependent on a |
| 10 | +precise DOM structure \(as for example the ones we get when using flex containers, margins, the |
| 11 | +child combinator selector, etc.\). |
| 12 | + |
| 13 | +## Usage notes |
| 14 | + |
| 15 | +### With `*NgIf`s |
| 16 | + |
| 17 | +One common use case of `<ng-container>` is alongside the `*ngIf` structural directive. By using the |
| 18 | +special element we can produce very clean templates easy to understand and work with. |
| 19 | + |
| 20 | +For example, we may want to have a number of elements shown conditionally but they do not need to be |
| 21 | +all under the same root element. That can be easily done by wrapping them in such a block: |
| 22 | + |
| 23 | +<code-example format="html" language="html"> |
| 24 | + |
| 25 | +<ng-container *ngIf="condition"> |
| 26 | + … |
| 27 | +</ng-container> |
| 28 | + |
| 29 | +</code-example> |
| 30 | + |
| 31 | +This can also be augmented with an `else` statement alongside an `<ng-template>` as: |
| 32 | + |
| 33 | +<code-example format="html" language="html"> |
| 34 | + |
| 35 | +<ng-container *ngIf="condition; else templateA"> |
| 36 | + … |
| 37 | +</ng-container> |
| 38 | +<ng-template #templateA> |
| 39 | + … |
| 40 | +</ng-template> |
| 41 | + |
| 42 | +</code-example> |
| 43 | + |
| 44 | +### Combination of multiple structural directives |
| 45 | + |
| 46 | +Multiple structural directives cannot be used on the same element; if you need to take advantage of |
| 47 | +more than one structural directive, it is advised to use an `<ng-container>` per structural |
| 48 | +directive. |
| 49 | + |
| 50 | +The most common scenario is with `*ngIf` and `*ngFor`. For example, let's imagine that we have a |
| 51 | +list of items but each item needs to be displayed only if a certain condition is true. We could be |
| 52 | +tempted to try something like: |
| 53 | + |
| 54 | +<code-example format="html" language="html"> |
| 55 | + |
| 56 | +<ul> |
| 57 | + <li *ngFor="let item of items" *ngIf="item.isValid"> |
| 58 | + {{ item.name }} |
| 59 | + </li> |
| 60 | +</ul> |
| 61 | + |
| 62 | +</code-example> |
| 63 | + |
| 64 | +As we said that would not work, what we can do is to simply move one of the structural directives to |
| 65 | +an `<ng-container>` element, which would then wrap the other one, like so: |
| 66 | + |
| 67 | +<code-example format="html" language="html"> |
| 68 | + |
| 69 | +<ul> |
| 70 | + <ng-container *ngFor="let item of items"> |
| 71 | + <li *ngIf="item.isValid"> |
| 72 | + {{ item.name }} |
| 73 | + </li> |
| 74 | + </ng-container> |
| 75 | +</ul> |
| 76 | + |
| 77 | +</code-example> |
| 78 | + |
| 79 | +This would work as intended without introducing any new unnecessary elements in the DOM. |
| 80 | + |
| 81 | +For more information see [one structural directive per element](guide/structural-directives#one-per-element). |
| 82 | + |
| 83 | +### Use alongside ngTemplateOutlet |
| 84 | + |
| 85 | +The `NgTemplateOutlet` directive can be applied to any element but most of the time it's applied |
| 86 | +to `<ng-container>` ones. By combining the two, we get a very clear and easy to follow HTML and DOM |
| 87 | +structure in which no extra elements are necessary and template views are instantiated where |
| 88 | +requested. |
| 89 | + |
| 90 | +For example, imagine a situation in which we have a large HTML, in which a small portion needs to be |
| 91 | +repeated in different places. A simple solution is to define an `<ng-template>` containing our |
| 92 | +repeating HTML and render that where necessary by using `<ng-container>` alongside |
| 93 | +an `NgTemplateOutlet`. |
| 94 | + |
| 95 | +Like so: |
| 96 | + |
| 97 | +<code-example format="html" language="html"> |
| 98 | + |
| 99 | +<!-- … --> |
| 100 | + |
| 101 | +<ng-container *ngTemplateOutlet="tmpl; context: {$implicit: 'Hello'}"> |
| 102 | +</ng-container> |
| 103 | + |
| 104 | +<!-- … --> |
| 105 | + |
| 106 | +<ng-container *ngTemplateOutlet="tmpl; context: {$implicit: 'World'}"> |
| 107 | +</ng-container> |
| 108 | + |
| 109 | +<!-- … --> |
| 110 | + |
| 111 | +<ng-template #tmpl let-text> |
| 112 | + <h1>{{ text }}</h1> |
| 113 | +</ng-template> |
| 114 | + |
| 115 | +</code-example> |
| 116 | + |
| 117 | +For more information regarding `NgTemplateOutlet`, see |
| 118 | +the [`NgTemplateOutlet`s api documentation page](api/common/NgTemplateOutlet). |
| 119 | + |
0 commit comments