|
| 1 | +<!-- #docplaster --> |
| 2 | +<!-- #docregion --> |
| 3 | +<h1><ng-container></h1> |
| 4 | + |
| 5 | +<!-- #docregion ngif --> |
| 6 | +<div *ngIf="hero">{{hero.name}}</div> |
| 7 | +<!-- #enddocregion ngif --> |
| 8 | + |
| 9 | +<hr> |
| 10 | + |
| 11 | +<h3><ng-container> and CSS</h3> |
| 12 | +<p>Examples demonstrating issues with rigid CSS styles.</p> |
| 13 | + |
| 14 | +<button (click)="hero = hero ? null : heroes[0]">Toggle hero</button> |
| 15 | + |
| 16 | +<h4>#1 <ng-container> and <p></h4> |
| 17 | +<!-- #docregion ngif-ngcontainer --> |
| 18 | +<p> |
| 19 | + I turned the corner |
| 20 | + <ng-container *ngIf="hero"> |
| 21 | + and saw {{hero.name}}. I waved |
| 22 | + </ng-container> |
| 23 | + and continued on my way. |
| 24 | +</p> |
| 25 | +<!-- #enddocregion ngif-ngcontainer --> |
| 26 | +<!-- #docregion ngif-span --> |
| 27 | +<p> |
| 28 | + I turned the corner |
| 29 | + <span *ngIf="hero"> |
| 30 | + and saw {{hero.name}}. I waved |
| 31 | + </span> |
| 32 | + and continued on my way. |
| 33 | +</p> |
| 34 | +<!-- #enddocregion ngif-span --> |
| 35 | + |
| 36 | +<h4>#2 <ng-container> and <p></h4> |
| 37 | + |
| 38 | +<div *ngIf="hero"> |
| 39 | + <!-- #docregion ngif-ngcontainer-2 --> |
| 40 | + <p> |
| 41 | + {{hero.name}} is |
| 42 | + <ng-container *ngFor="let trait of heroTraits; let first=first; let last=last"> |
| 43 | + <ng-container *ngIf="!first">,</ng-container> |
| 44 | + <ng-container *ngIf="last">and</ng-container> |
| 45 | + {{trait}} |
| 46 | + </ng-container>. |
| 47 | + </p> |
| 48 | + <!-- #enddocregion ngif-ngcontainer-2 --> |
| 49 | + |
| 50 | + <!-- #docregion ngif-span-2 --> |
| 51 | + <p> |
| 52 | + {{hero.name}} is |
| 53 | + <span *ngFor="let trait of heroTraits; let first=first; let last=last"> |
| 54 | + <span *ngIf="!first">,</span> |
| 55 | + <span *ngIf="last">and</span> |
| 56 | + {{trait}} |
| 57 | + </span>. |
| 58 | + </p> |
| 59 | + <!-- #enddocregion ngif-span-2 --> |
| 60 | + |
| 61 | +<br> |
| 62 | + |
| 63 | +<h4>#3 <ng-container> and <p></h4> |
| 64 | + <!-- #docregion ngif-span-3 --> |
| 65 | + <p> |
| 66 | + <label><input type="checkbox" [checked]="showId" (change)="showId=!showId">Show ID</label> |
| 67 | + </p> |
| 68 | + <!-- #enddocregion ngif-span-3 --> |
| 69 | + |
| 70 | + <div> |
| 71 | + The <code>hero.id</code> in the <span> |
| 72 | + is caught by the <span>p-span</span> CSS: |
| 73 | + <!-- #docregion ngif-span-3 --> |
| 74 | + <p> |
| 75 | + <span *ngIf="showId"> |
| 76 | + Id: ({{hero.id}}) |
| 77 | + </span> |
| 78 | + Name: {{hero.name}} |
| 79 | + </p> |
| 80 | + <!-- #enddocregion ngif-span-3 --> |
| 81 | + </div> |
| 82 | + |
| 83 | + <div> |
| 84 | + The <code>hero.id</code> in the <ng-container> |
| 85 | + is unaffected by the <span>p-span</span> CSS: |
| 86 | + <p> |
| 87 | + <ng-container *ngIf="showId"> |
| 88 | + Id: ({{hero.id}}) |
| 89 | + </ng-container> |
| 90 | + Name: {{hero.name}} |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + |
| 94 | + <div> |
| 95 | + The <code>hero.id</code> in the <template *ngIf> disappears: |
| 96 | + <p> |
| 97 | + <template *ngIf="showId"> |
| 98 | + Id: ({{hero.id}}) |
| 99 | + </template> |
| 100 | + Name: {{hero.name}} |
| 101 | + </p> |
| 102 | + </div> |
| 103 | + |
| 104 | + <div> |
| 105 | + The <code>hero.id</code> in the <template [ngIf]> |
| 106 | + is unaffected by the <span>p-span</span> CSS: |
| 107 | + <p> |
| 108 | + <template [ngIf]="showId"> |
| 109 | + Id: ({{hero.id}}) |
| 110 | + </template> |
| 111 | + Name: {{hero.name}} |
| 112 | + </p> |
| 113 | + </div> |
| 114 | + |
| 115 | +</div> |
| 116 | + |
| 117 | +<hr> |
| 118 | + |
| 119 | +<h3><ng-container> and layout-sensitive elements</h3> |
| 120 | +<p> |
| 121 | + Examples demonstrating issues with layout-sensitive elements |
| 122 | + such as <select> and <table>. |
| 123 | +</p> |
| 124 | + |
| 125 | +<h4>#1 <ng-container> and <options></h4> |
| 126 | + |
| 127 | +<p><i><select> with <span></i></p> |
| 128 | +<div> |
| 129 | + Pick your favorite hero |
| 130 | + (<label><input type="checkbox" checked (change)="showSad=!showSad">show sad</label>) |
| 131 | +</div> |
| 132 | +<!-- #docregion select-span --> |
| 133 | +<select [(ngModel)]="hero"> |
| 134 | + <span *ngFor="let h of heroes"> |
| 135 | + <span *ngIf="showSad || h?.emotion != 'sad'"> |
| 136 | + <option [ngValue]="h">{{h.name}} ({{h?.emotion}})</option> |
| 137 | + </span> |
| 138 | + </span> |
| 139 | +</select> |
| 140 | +<!-- #enddocregion select-span --> |
| 141 | + |
| 142 | +<p><i><select> with <ng-container></i></p> |
| 143 | +<div> |
| 144 | + Pick your favorite hero |
| 145 | + (<label><input type="checkbox" checked (change)="showSad=!showSad">show sad</label>) |
| 146 | +</div> |
| 147 | +<!-- #docregion select-ngcontainer --> |
| 148 | +<select [(ngModel)]="hero"> |
| 149 | + <ng-container *ngFor="let h of heroes"> |
| 150 | + <ng-container *ngIf="showSad || h?.emotion != 'sad'"> |
| 151 | + <option [ngValue]="h">{{h.name}} ({{h?.emotion}})</option> |
| 152 | + </ng-container> |
| 153 | + </ng-container> |
| 154 | +</select> |
| 155 | +<!-- #enddocregion select-ngcontainer --> |
| 156 | + |
| 157 | +<br><br><br><br> |
| 158 | + |
| 159 | +<h4>#2 <ng-container> and <options></h4> |
| 160 | +<p> |
| 161 | + <label (change)="traitPicker.value=showDefaultTraits ? 'loyal' : heroTraits[0]"> |
| 162 | + <input type="checkbox" |
| 163 | + [checked]="showDefaultTraits" |
| 164 | + (change)="showDefaultTraits=!showDefaultTraits">Show default traits |
| 165 | + </label> |
| 166 | +</p> |
| 167 | + |
| 168 | +<p>Options with <ng-container></p> |
| 169 | + |
| 170 | +<select #traitPicker> |
| 171 | + <!-- #docregion select-ngcontainer-2 --> |
| 172 | + <ng-container *ngIf="showDefaultTraits"> |
| 173 | + <!-- Default traits --> |
| 174 | + <option value="loyal">LOYAL</option> |
| 175 | + <option value="clean">CLEAN</option> |
| 176 | + <option value="reverent">REVERENT</option> |
| 177 | + </ng-container> |
| 178 | + <option *ngFor="let trait of heroTraits" [value]="trait"> |
| 179 | + {{ trait | uppercase }} |
| 180 | + </option> |
| 181 | + <!-- #enddocregion select-ngcontainer-2 --> |
| 182 | +</select> |
| 183 | + |
| 184 | + |
| 185 | +<p>Options with <span></p> |
| 186 | +<!-- #docregion select-span-2 --> |
| 187 | +<select> |
| 188 | + <!-- Default traits are always excluded because intermediate <span> is illegal --> |
| 189 | + <span *ngIf="showDefaultTraits"> |
| 190 | + <option value="loyal">LOYAL</option> |
| 191 | + <option value="clean">CLEAN</option> |
| 192 | + <option value="reverent">REVERENT</option> |
| 193 | + </span> |
| 194 | + <option *ngFor="let trait of heroTraits" [value]="trait"> |
| 195 | + {{ trait | uppercase }} |
| 196 | + </option> |
| 197 | +</select> |
| 198 | +<!-- #enddocregion select-span-2 --> |
| 199 | + |
| 200 | +<br> |
| 201 | + |
| 202 | +<h4><ng-container> and <table></h4> |
| 203 | +<p> |
| 204 | + <label><input type="checkbox" checked (change)="attrDirs=!attrDirs">Attribute directives</label> |
| 205 | + <label><input type="checkbox" checked (change)="strucDirs=!strucDirs">Structural directives</label> |
| 206 | + <label><input type="checkbox" (change)="divNgIf=!divNgIf">div with *ngIf</label> |
| 207 | +</p> |
| 208 | + |
| 209 | +<table> |
| 210 | + <tr> |
| 211 | + <th width="20%">Directive</th> |
| 212 | + <th width="10%">Type</th> |
| 213 | + <th>Description</th> |
| 214 | + </tr> |
| 215 | + |
| 216 | + <tr *ngIf="attrDirs"> |
| 217 | + <td>NgClass</td><td>A</td><td>Add or remove multiple CSS classes.</td> |
| 218 | + </tr> |
| 219 | + |
| 220 | +<ng-container *ngIf="divNgIf"> |
| 221 | + <!-- #docregion ngif-div --> |
| 222 | + <div *ngIf="strucDirs"> |
| 223 | + <tr> |
| 224 | + <td>xxx</td><td>S</td><td>div with *ngIf formats crazy.</td> |
| 225 | + </tr> |
| 226 | + <tr> |
| 227 | + <td>yyy</td><td>S</td><td>div with *ngIf formats crazy.</td> |
| 228 | + </tr> |
| 229 | + </div> |
| 230 | + <!-- #enddocregion ngif-div --> |
| 231 | +</ng-container> |
| 232 | + |
| 233 | + <!-- #docregion ngif-ngcontainer-4 --> |
| 234 | + <ng-container *ngIf="strucDirs"> |
| 235 | + <tr> |
| 236 | + <td>NgFor</td><td>S</td><td>Repeat the template for each item in a list.</td> |
| 237 | + </tr> |
| 238 | + <tr> |
| 239 | + <td>NgIf</td><td>S</td><td>Add or remove DOM elements.</td> |
| 240 | + </tr> |
| 241 | + </ng-container> |
| 242 | + <!-- #enddocregion ngif-ngcontainer-4 --> |
| 243 | + |
| 244 | + <ng-container *ngIf="attrDirs"> |
| 245 | + <tr> |
| 246 | + <td>NgStyle</td><td>A</td><td>Add or remove multiple style attributes.</td> |
| 247 | + </tr> |
| 248 | + </ng-container> |
| 249 | + |
| 250 | + <!-- #docregion ngif-tr --> |
| 251 | + <tr *ngIf="strucDirs"> |
| 252 | + <td>NgSwitch</td><td>S</td><td>Include in DOM if case matches the switch value.</td> |
| 253 | + </tr> |
| 254 | + <!-- #enddocregion ngif-tr --> |
| 255 | + |
| 256 | +</table> |
| 257 | + |
| 258 | +<hr> |
| 259 | + |
| 260 | +<h3>Do not confuse <ng-container> with <ng-content></h3> |
| 261 | + |
| 262 | +<p><ng-container>Inside ng-container</ng-container></p> |
| 263 | +<!-- #docregion ngcontainer-bare --> |
| 264 | +<ng-container>Inside ng-container</ng-container> |
| 265 | +<!-- #enddocregion ngcontainer-bare --> |
| 266 | + |
| 267 | +<p><ng-content>this is an Angular parse error</ng-content></p> |
| 268 | +<!-- #docregion ngcontent-bad --> |
| 269 | +<!-- <ng-content>this is an Angular parse error</ng-content> --> |
| 270 | +<!-- #enddocregion ngcontent-bad --> |
| 271 | +<div class="code">Template parse errors:<br> |
| 272 | +<ng-content> element cannot have content.</div> |
| 273 | + |
| 274 | +<p>Demo of </ng-content></p> |
| 275 | +<!-- #docregion content-comp --> |
| 276 | +<content-comp> |
| 277 | + Projected content |
| 278 | +</content-comp> |
| 279 | +<!-- #enddocregion content-comp --> |
0 commit comments