| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {Component, NgModule, OnInit, TemplateRef, ViewChild} from '@angular/core'; | 
					
						
							|  |  |  | import {BrowserModule} from '@angular/platform-browser'; | 
					
						
							| 
									
										
										
										
											2018-02-27 17:06:06 -05:00
										 |  |  | import {Subject} from 'rxjs'; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion NgIfSimple
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'ng-if-simple', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |     <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button> | 
					
						
							|  |  |  |     show = {{show}} | 
					
						
							|  |  |  |     <br> | 
					
						
							|  |  |  |     <div *ngIf="show">Text to show</div> | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  | export class NgIfSimple { | 
					
						
							| 
									
										
										
										
											2020-10-01 21:29:29 +05:30
										 |  |  |   show = true; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion NgIfElse
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'ng-if-else', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |     <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button> | 
					
						
							|  |  |  |     show = {{show}} | 
					
						
							|  |  |  |     <br> | 
					
						
							|  |  |  |     <div *ngIf="show; else elseBlock">Text to show</div> | 
					
						
							| 
									
										
										
										
											2017-01-09 13:16:46 -08:00
										 |  |  |     <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template> | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | `
 | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  | export class NgIfElse { | 
					
						
							| 
									
										
										
										
											2020-10-01 21:29:29 +05:30
										 |  |  |   show = true; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion NgIfThenElse
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'ng-if-then-else', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |     <button (click)="show = !show">{{show ? 'hide' : 'show'}}</button> | 
					
						
							|  |  |  |     <button (click)="switchPrimary()">Switch Primary</button> | 
					
						
							|  |  |  |     show = {{show}} | 
					
						
							|  |  |  |     <br> | 
					
						
							|  |  |  |     <div *ngIf="show; then thenBlock; else elseBlock">this is ignored</div> | 
					
						
							| 
									
										
										
										
											2017-01-09 13:16:46 -08:00
										 |  |  |     <ng-template #primaryBlock>Primary text to show</ng-template> | 
					
						
							|  |  |  |     <ng-template #secondaryBlock>Secondary text to show</ng-template> | 
					
						
							|  |  |  |     <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template> | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | `
 | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  | export class NgIfThenElse implements OnInit { | 
					
						
							| 
									
										
										
										
											2017-03-24 09:57:05 -07:00
										 |  |  |   thenBlock: TemplateRef<any>|null = null; | 
					
						
							| 
									
										
										
										
											2020-10-01 21:29:29 +05:30
										 |  |  |   show = true; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   @ViewChild('primaryBlock', {static: true}) primaryBlock: TemplateRef<any>|null = null; | 
					
						
							|  |  |  |   @ViewChild('secondaryBlock', {static: true}) secondaryBlock: TemplateRef<any>|null = null; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   switchPrimary() { | 
					
						
							|  |  |  |     this.thenBlock = this.thenBlock === this.primaryBlock ? this.secondaryBlock : this.primaryBlock; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   ngOnInit() { | 
					
						
							|  |  |  |     this.thenBlock = this.primaryBlock; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 20:46:29 -07:00
										 |  |  | // #docregion NgIfAs
 | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2020-03-04 12:15:16 +00:00
										 |  |  |   selector: 'ng-if-as', | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |   template: `
 | 
					
						
							|  |  |  |     <button (click)="nextUser()">Next User</button> | 
					
						
							|  |  |  |     <br> | 
					
						
							| 
									
										
										
										
											2017-03-14 20:46:29 -07:00
										 |  |  |     <div *ngIf="userObservable | async as user; else loading"> | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |       Hello {{user.last}}, {{user.first}}! | 
					
						
							|  |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2017-01-09 13:16:46 -08:00
										 |  |  |     <ng-template #loading let-user>Waiting... (user is {{user|json}})</ng-template> | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | `
 | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  | export class NgIfAs { | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |   userObservable = new Subject<{first: string, last: string}>(); | 
					
						
							|  |  |  |   first = ['John', 'Mike', 'Mary', 'Bob']; | 
					
						
							|  |  |  |   firstIndex = 0; | 
					
						
							|  |  |  |   last = ['Smith', 'Novotny', 'Angular']; | 
					
						
							|  |  |  |   lastIndex = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   nextUser() { | 
					
						
							| 
									
										
										
										
											2016-12-09 14:42:39 -08:00
										 |  |  |     let first = this.first[this.firstIndex++]; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |     if (this.firstIndex >= this.first.length) this.firstIndex = 0; | 
					
						
							| 
									
										
										
										
											2016-12-09 14:42:39 -08:00
										 |  |  |     let last = this.last[this.lastIndex++]; | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |     if (this.lastIndex >= this.last.length) this.lastIndex = 0; | 
					
						
							|  |  |  |     this.userObservable.next({first, last}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'example-app', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |     <ng-if-simple></ng-if-simple> | 
					
						
							|  |  |  |     <hr> | 
					
						
							|  |  |  |     <ng-if-else></ng-if-else> | 
					
						
							|  |  |  |     <hr> | 
					
						
							|  |  |  |     <ng-if-then-else></ng-if-then-else> | 
					
						
							|  |  |  |     <hr> | 
					
						
							| 
									
										
										
										
											2020-03-04 12:15:16 +00:00
										 |  |  |     <ng-if-as></ng-if-as> | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  |     <hr> | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  | export class AppComponent { | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @NgModule({ | 
					
						
							|  |  |  |   imports: [BrowserModule], | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  |   declarations: [AppComponent, NgIfSimple, NgIfElse, NgIfThenElse, NgIfAs], | 
					
						
							| 
									
										
										
										
											2016-12-07 21:41:27 -08:00
										 |  |  | }) | 
					
						
							|  |  |  | export class AppModule { | 
					
						
							|  |  |  | } |