| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-04-22 07:22:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 05:51:41 -07:00
										 |  |  |   h2#section-its-all-a-tree It's all a tree | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  |   p. | 
					
						
							|  |  |  |       As mentioned earlier, you build Angular applications as a tree of nested components.  You've seen how to create | 
					
						
							|  |  |  |       a top-level component.  You add child components to a parent component by using them in the parent component's | 
					
						
							|  |  |  |       template. | 
					
						
							|  |  |  |   p. | 
					
						
							|  |  |  |       Given a bootstrapping template with a <code><parent></code> tag in the body, you can create a parent | 
					
						
							|  |  |  |       component that uses a <code><child></code> component like so: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |   code-tabs | 
					
						
							|  |  |  |     code-pane(language="javascript" name="TypeScript" format="linenums"). | 
					
						
							|  |  |  |       //TypeScript | 
					
						
							|  |  |  |       @Component({ | 
					
						
							|  |  |  |         selector: 'parent' | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       @View({ | 
					
						
							|  |  |  |         template: ` | 
					
						
							|  |  |  |           <h1>{{ message }}</h1> | 
					
						
							|  |  |  |           <child></child> | 
					
						
							|  |  |  |         `, | 
					
						
							|  |  |  |         directives: [ChildComponent] | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       class ParentComponent { | 
					
						
							|  |  |  |         message: string; | 
					
						
							| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |         constructor() { | 
					
						
							| 
									
										
										
										
											2015-04-22 07:22:30 -07:00
										 |  |  |           this.message = "I'm the parent"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     code-pane(language="javascript" name="ES5" format="linenums"). | 
					
						
							|  |  |  |       //ES5 | 
					
						
							|  |  |  |       function ParentComponent() { | 
					
						
							|  |  |  |         this.message = "I'm the parent"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       ParentComponent.annotations = [ | 
					
						
							|  |  |  |         new angular.ComponentAnnotation({ | 
					
						
							|  |  |  |           selector: "parent" | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         new angular.ViewAnnotation({ | 
					
						
							|  |  |  |           template: | 
					
						
							|  |  |  |             '<h1>{{ message }}</h1>' + | 
					
						
							|  |  |  |             '<child></child>', | 
					
						
							| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  |           directives: [ChildComponent] | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   p You then just need to write the <code>ChildComponent</code> class to make it work: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |   code-tabs | 
					
						
							|  |  |  |     code-pane(language="javascript" name="TypeScript" format="linenums"). | 
					
						
							|  |  |  |       //TypeScript | 
					
						
							|  |  |  |       @Component({ | 
					
						
							|  |  |  |         selector: 'child' | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       @View({ | 
					
						
							|  |  |  |         template: ` | 
					
						
							|  |  |  |           <p> {{ message }} </p> | 
					
						
							|  |  |  |         ` | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       class ChildComponent { | 
					
						
							|  |  |  |         message: string; | 
					
						
							|  |  |  |         constructor() { | 
					
						
							| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  |           this.message = "I'm the child"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     code-pane(language="javascript" name="ES5" format="linenums"). | 
					
						
							|  |  |  |       //ES5 | 
					
						
							|  |  |  |       function ChildComponent() { | 
					
						
							|  |  |  |         this.message = "I'm the child"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       ChildComponent.annotations = [ | 
					
						
							|  |  |  |         new angular.ComponentAnnotation({ | 
					
						
							|  |  |  |           selector: "child" | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         new angular.ViewAnnotation({ | 
					
						
							|  |  |  |           template: '<p> {{ message }} </p>' | 
					
						
							| 
									
										
										
										
											2015-04-22 07:22:30 -07:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2015-05-19 06:33:09 -07:00
										 |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2015-05-04 22:14:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 02:29:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   p. | 
					
						
							|  |  |  |       Notice that in addition to using the <code><child></code> element in the parent template, you also need to | 
					
						
							| 
									
										
										
										
											2015-04-22 05:55:25 -07:00
										 |  |  |       add <code>ChildComponent</code> in <code>ParentComponent</code>'s list of directives. |