| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #docplaster
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | import { AfterViewChecked, AfterViewInit, Component, ViewChild } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  | import { LoggerService }  from './logger.service'; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | //////////////////
 | 
					
						
							|  |  |  | // #docregion child-view
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   selector: 'my-child-view', | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   template: '<input [(ngModel)]="hero">' | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | export class ChildViewComponent { | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   hero = 'Magneta'; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion child-view
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | //////////////////////
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'after-view', | 
					
						
							|  |  |  | // #docregion template
 | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |     <div>-- child view begins --</div> | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |       <my-child-view></my-child-view> | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     <div>-- child view ends --</div>`
 | 
					
						
							|  |  |  | // #enddocregion template
 | 
					
						
							|  |  |  |    + `
 | 
					
						
							|  |  |  |     <p *ngIf="comment" class="comment"> | 
					
						
							|  |  |  |       {{comment}} | 
					
						
							|  |  |  |     </p> | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   `
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | }) | 
					
						
							|  |  |  | // #docregion hooks
 | 
					
						
							|  |  |  | export class AfterViewComponent implements  AfterViewChecked, AfterViewInit { | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   private prevHero = ''; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   // Query for a VIEW child of type `ChildViewComponent`
 | 
					
						
							|  |  |  |   @ViewChild(ChildViewComponent) viewChild: ChildViewComponent; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #enddocregion hooks
 | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   constructor(private logger: LoggerService) { | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     this.logIt('AfterView constructor'); | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #docregion hooks
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   ngAfterViewInit() { | 
					
						
							|  |  |  |     // viewChild is set after the view has been initialized
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     this.logIt('AfterViewInit'); | 
					
						
							|  |  |  |     this.doSomething(); | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ngAfterViewChecked() { | 
					
						
							|  |  |  |     // viewChild is updated after the view has been checked
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     if (this.prevHero === this.viewChild.hero) { | 
					
						
							|  |  |  |       this.logIt('AfterViewChecked (no change)'); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |       this.prevHero = this.viewChild.hero; | 
					
						
							|  |  |  |       this.logIt('AfterViewChecked'); | 
					
						
							|  |  |  |       this.doSomething(); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | // #enddocregion hooks
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   comment = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion do-something
 | 
					
						
							|  |  |  |   // This surrogate for real business logic sets the `comment`
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   private doSomething() { | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |     let c = this.viewChild.hero.length > 10 ? `That's a long name` : ''; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     if (c !== this.comment) { | 
					
						
							|  |  |  |       // Wait a tick because the component's view has already been checked
 | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |       this.logger.tick_then(() => this.comment = c); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #enddocregion do-something
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   private logIt(method: string) { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     let child = this.viewChild; | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |     let message = `${method}: ${child ? child.hero : 'no'} child view`; | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     this.logger.log(message); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | // #docregion hooks
 | 
					
						
							|  |  |  |   // ...
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion hooks
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //////////////
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'after-view-parent', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |   <div class="parent"> | 
					
						
							|  |  |  |     <h2>AfterView</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <after-view  *ngIf="show"></after-view> | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     <h4>-- AfterView Logs --</h4> | 
					
						
							|  |  |  |     <p><button (click)="reset()">Reset</button></p> | 
					
						
							| 
									
										
										
										
											2016-04-29 01:36:35 +01:00
										 |  |  |     <div *ngFor="let msg of logs">{{msg}}</div> | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   </div> | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   styles: ['.parent {background: burlywood}'], | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   providers: [LoggerService] | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | }) | 
					
						
							|  |  |  | export class AfterViewParentComponent { | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   logs: string[]; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   show = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |   constructor(private logger: LoggerService) { | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     this.logs = logger.logs; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   reset() { | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |     this.logs.length = 0; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     // quickly remove and reload AfterViewComponent which recreates it
 | 
					
						
							|  |  |  |     this.show = false; | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     this.logger.tick_then(() => this.show = true); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | } |