| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  | import { | 
					
						
							|  |  |  |   Component, | 
					
						
							|  |  |  |   ViewChildren, | 
					
						
							|  |  |  |   ContentChild, | 
					
						
							|  |  |  |   QueryList, | 
					
						
							|  |  |  |   Input | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'active-label', | 
					
						
							|  |  |  |   template: `<span class="active-label"
 | 
					
						
							|  |  |  |                    *ngIf="active"> | 
					
						
							|  |  |  |     Active | 
					
						
							|  |  |  |   </span>`
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class ActiveLabelComponent { | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   active: boolean; | 
					
						
							| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   activate() { | 
					
						
							|  |  |  |     this.active = true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion content
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'hero', | 
					
						
							|  |  |  |   template: `<h2 [class.active]=active>
 | 
					
						
							|  |  |  |     {{hero.name}} | 
					
						
							|  |  |  |     <ng-content></ng-content> | 
					
						
							|  |  |  |   </h2>`
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class HeroComponent { | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   @Input() hero: any; | 
					
						
							|  |  |  |   active: boolean; | 
					
						
							| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @ContentChild(ActiveLabelComponent) | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   label: ActiveLabelComponent; | 
					
						
							| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   activate() { | 
					
						
							|  |  |  |     this.active = true; | 
					
						
							|  |  |  |     this.label.activate(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion content
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion view
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'heroes-queries', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							| 
									
										
										
										
											2016-04-29 01:36:35 +01:00
										 |  |  |     <hero *ngFor="let hero of heroData" | 
					
						
							| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  |           [hero]="hero"> | 
					
						
							|  |  |  |       <active-label></active-label> | 
					
						
							|  |  |  |     </hero> | 
					
						
							|  |  |  |     <button (click)="activate()"> | 
					
						
							|  |  |  |       Activate | 
					
						
							|  |  |  |     </button> | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   directives: [ | 
					
						
							|  |  |  |     HeroComponent, | 
					
						
							|  |  |  |     ActiveLabelComponent | 
					
						
							|  |  |  |   ] | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | export class HeroesQueriesComponent { | 
					
						
							|  |  |  |   heroData = [ | 
					
						
							|  |  |  |     {id: 1, name: 'Windstorm'}, | 
					
						
							|  |  |  |     {id: 2, name: 'Superman'} | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ViewChildren(HeroComponent) | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  |   heroCmps: QueryList<HeroComponent>; | 
					
						
							| 
									
										
										
										
											2016-03-16 18:01:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   activate() { | 
					
						
							|  |  |  |     this.heroCmps.forEach( | 
					
						
							|  |  |  |       (cmp) => cmp.activate() | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion view
 |