| 
									
										
										
										
											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 { AfterContentChecked, AfterContentInit, Component, ContentChild } 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
										 |  |  | //////////////////
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   selector: 'my-child', | 
					
						
							|  |  |  |   template: '<input [(ngModel)]="hero">' | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | export class ChildComponent { | 
					
						
							|  |  |  |   hero = 'Magneta'; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | //////////////////////
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'after-content', | 
					
						
							|  |  |  | // #docregion template
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   template: `
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     <div>-- projected content begins --</div> | 
					
						
							|  |  |  |       <ng-content></ng-content> | 
					
						
							|  |  |  |     <div>-- projected content ends --</div>`
 | 
					
						
							|  |  |  | // #enddocregion template
 | 
					
						
							|  |  |  |    + `
 | 
					
						
							|  |  |  |     <p *ngIf="comment" class="comment"> | 
					
						
							|  |  |  |       {{comment}} | 
					
						
							|  |  |  |     </p> | 
					
						
							|  |  |  |   `
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | // #docregion hooks
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | export class AfterContentComponent implements AfterContentChecked, AfterContentInit { | 
					
						
							|  |  |  |   private prevHero = ''; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   comment = ''; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Query for a CONTENT child of type `ChildComponent`
 | 
					
						
							|  |  |  |   @ContentChild(ChildComponent) contentChild: ChildComponent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #enddocregion hooks
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   constructor(private logger: LoggerService) { | 
					
						
							|  |  |  |     this.logIt('AfterContent constructor'); | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #docregion hooks
 | 
					
						
							|  |  |  |   ngAfterContentInit() { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     // contentChild is set after the content has been initialized
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     this.logIt('AfterContentInit'); | 
					
						
							|  |  |  |     this.doSomething(); | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ngAfterContentChecked() { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     // contentChild is updated after the content has been checked
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     if (this.prevHero === this.contentChild.hero) { | 
					
						
							|  |  |  |       this.logIt('AfterContentChecked (no change)'); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |       this.prevHero = this.contentChild.hero; | 
					
						
							|  |  |  |       this.logIt('AfterContentChecked'); | 
					
						
							|  |  |  |       this.doSomething(); | 
					
						
							| 
									
										
										
										
											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 hooks
 | 
					
						
							|  |  |  | // #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
										 |  |  |     this.comment = this.contentChild.hero.length > 10 ? `That's a long name` : ''; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   private logIt(method: string) { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     let child = this.contentChild; | 
					
						
							|  |  |  |     let message = `${method}: ${child ? child.hero : 'no'} child content`; | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     this.logger.log(message); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | // #docregion hooks
 | 
					
						
							|  |  |  |   // ...
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #enddocregion hooks
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | //////////////
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'after-content-parent', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |   <div class="parent"> | 
					
						
							|  |  |  |     <h2>AfterContent</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     <div *ngIf="show">` +
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #docregion parent-template
 | 
					
						
							|  |  |  |      `<after-content>
 | 
					
						
							|  |  |  |         <my-child></my-child> | 
					
						
							|  |  |  |       </after-content>` | 
					
						
							|  |  |  | // #enddocregion parent-template
 | 
					
						
							|  |  |  | + `</div>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <h4>-- AfterContent 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> | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   </div> | 
					
						
							|  |  |  |   `,
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   styles: ['.parent {background: burlywood}'], | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   providers: [LoggerService] | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | }) | 
					
						
							|  |  |  | export class AfterContentParentComponent { | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   logs: string[]; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   show = true; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |   constructor(private logger: LoggerService) { | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     this.logs = logger.logs; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reset() { | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |     this.logs.length = 0; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     // quickly remove and reload AfterContentComponent which recreates it
 | 
					
						
							|  |  |  |     this.show = false; | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |     this.logger.tick_then(() => this.show = true); | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |