| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <!-- #docplaster --> | 
					
						
							|  |  |  | <a id="toc"></a> | 
					
						
							|  |  |  | <h1>Template Syntax</h1> | 
					
						
							|  |  |  | <a href="#interpolation">Interpolation</a><br> | 
					
						
							|  |  |  | <a href="#mental-model">Mental Model</a><br> | 
					
						
							|  |  |  | <a href="#buttons">Buttons</a><br> | 
					
						
							|  |  |  | <a href="#prop-vs-attrib">Properties vs. Attributes</a><br> | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <a href="#property-binding">Property Binding</a><br> | 
					
						
							|  |  |  | <div style="margin-left:8px"> | 
					
						
							|  |  |  |   <a href="#attribute-binding">Attribute Binding</a><br> | 
					
						
							|  |  |  |   <a href="#class-binding">Class Binding</a><br> | 
					
						
							|  |  |  |   <a href="#style-binding">Style Binding</a><br> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <a href="#event-binding">Event Binding</a><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <div>Directives</div> | 
					
						
							|  |  |  | <div style="margin-left:8px"> | 
					
						
							|  |  |  |   <a href="#ngModel">NgModel (two-way) Binding</a><br> | 
					
						
							|  |  |  |   <a href="#ngClass">NgClass Binding</a><br> | 
					
						
							|  |  |  |   <a href="#ngStyle">NgStyle Binding</a><br> | 
					
						
							|  |  |  |   <a href="#ngIf">NgIf</a><br> | 
					
						
							|  |  |  |   <a href="#ngSwitch">NgSwitch</a><br> | 
					
						
							|  |  |  |   <a href="#ngFor">NgFor</a><br> | 
					
						
							|  |  |  |   <div style="margin-left:8px"> | 
					
						
							|  |  |  |     <a href="#ngFor-index">NgFor with index</a><br> | 
					
						
							|  |  |  |     <a href="#ngFor-trackBy">NgFor with trackBy</a><br> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <a href="#star-prefix">* prefix and <template></a><br> | 
					
						
							|  |  |  | <a href="#local-vars">Template local variables</a><br> | 
					
						
							|  |  |  | <a href="#inputs-and-outputs">Inputs and outputs</a><br> | 
					
						
							|  |  |  | <a href="#pipes">Pipes</a><br> | 
					
						
							|  |  |  | <a href="#elvis">Elvis <i>?.</i></a><br> | 
					
						
							|  |  |  | <!--<a href="#enums">Enums</a><br>--> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- Interpolation and expressions --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="interpolation">Interpolation</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion first-interpolation --> | 
					
						
							|  |  |  | <p>My current hero is {{currentHero.firstName}}</p> | 
					
						
							|  |  |  | <!-- #enddocregion first-interpolation --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion title+image --> | 
					
						
							|  |  |  | <h3> | 
					
						
							|  |  |  |   {{title}} | 
					
						
							|  |  |  |   <img src="{{heroImageUrl}}" style="height:30px"> | 
					
						
							|  |  |  | </h3> | 
					
						
							|  |  |  | <!-- #enddocregion title+image --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion sum-1 --> | 
					
						
							|  |  |  | <!-- "The sum of 1 + 1 is 2" --> | 
					
						
							|  |  |  | <p>The sum of 1 + 1 is {{1 + 1}}</p> | 
					
						
							|  |  |  | <!-- #enddocregion sum-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion sum-2 --> | 
					
						
							|  |  |  | <!-- "The sum of 1 + 1 is not 4" --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <p>The sum of 1 + 1 is not {{1 + 1 + getVal()}}</p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion sum-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- New Mental Model --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="mental-model">New Mental Model</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!--<img src="http://www.wpclipart.com/cartoon/people/hero/hero_silhoutte_T.png">--> | 
					
						
							|  |  |  | <!-- Public Domain terms of use: http://www.wpclipart.com/terms.html --> | 
					
						
							|  |  |  | <!-- #docregion img+button --> | 
					
						
							|  |  |  | <div class="special">Mental Model</div> | 
					
						
							|  |  |  | <img src="assets/images/hero.png"> | 
					
						
							|  |  |  | <button disabled>Save</button> | 
					
						
							|  |  |  | <!-- #enddocregion img+button --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion hero-detail-1 --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  |   <!-- Normal HTML --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |   <div class="special">Mental Model</div> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  |   <!-- Wow! A new element! --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |   <hero-detail></hero-detail> | 
					
						
							|  |  |  |   <!-- #enddocregion hero-detail-1 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion disabled-button-1 --> | 
					
						
							|  |  |  |   <!-- Bind button disabled state to `isUnchanged` property --> | 
					
						
							|  |  |  |   <button [disabled]="isUnchanged">Save</button> | 
					
						
							|  |  |  |   <!-- #enddocregion disabled-button-1 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion property-binding-syntax-1 --> | 
					
						
							|  |  |  |   <img [src] = "heroImageUrl"> | 
					
						
							|  |  |  |   <hero-detail [hero]="currentHero"></hero-detail> | 
					
						
							|  |  |  |   <div [ngClass] = "{selected: isSelected}"></div> | 
					
						
							|  |  |  |   <!-- #enddocregion property-binding-syntax-1 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion event-binding-syntax-1 --> | 
					
						
							|  |  |  | <button (click) = "onSave()">Save</button> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <hero-detail (deleteRequest)="deleteHero()"></hero-detail> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <div (myClick)="clickity=$event">click me</div> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion event-binding-syntax-1 --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | {{clickity}} | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion 2-way-binding-syntax-1 --> | 
					
						
							|  |  |  |   <input [(ngModel)]="heroName"> | 
					
						
							|  |  |  |   <!-- #enddocregion 2-way-binding-syntax-1 --> | 
					
						
							|  |  |  |   Hero Name: {{heroName}} | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion attribute-binding-syntax-1 --> | 
					
						
							|  |  |  | <button [attr.aria-label]="help">help</button> | 
					
						
							|  |  |  | <!-- #enddocregion attribute-binding-syntax-1 --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion class-binding-syntax-1 --> | 
					
						
							|  |  |  | <div [class.special]="isSpecial">Special</div> | 
					
						
							|  |  |  | <!-- #enddocregion class-binding-syntax-1 --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion style-binding-syntax-1 --> | 
					
						
							|  |  |  | <button [style.color] = "isSpecial ? 'red' : 'green'"> | 
					
						
							|  |  |  | <!-- #enddocregion style-binding-syntax-1 --> | 
					
						
							|  |  |  | button</button> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- property vs. attribute --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="prop-vs-attrib">Property vs. Attribute (img examples)</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- examine the following <img> tag in the browser tools --> | 
					
						
							|  |  |  | <img src="assets/images/ng-logo.png" | 
					
						
							|  |  |  |      [src]="heroImageUrl"> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <img [src]="iconUrl"/> | 
					
						
							|  |  |  | <img bind-src="heroImageUrl"/> | 
					
						
							|  |  |  | <img [attr.src]="villainImageUrl"/> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- buttons --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="buttons">Buttons</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <button>Enabled (but does nothing)</button> | 
					
						
							|  |  |  | <button disabled>Disabled</button> | 
					
						
							|  |  |  | <button disabled=false>Still disabled</button> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | <button disabled>disabled by attribute</button> | 
					
						
							|  |  |  | <button [disabled]="isUnchanged">disabled by property binding</button> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | <button bind-disabled="isUnchanged" on-click="onSave($event)">Disabled Cancel</button> | 
					
						
							|  |  |  | <button [disabled]="!canSave" (click)="onSave($event)">Enabled Save</button> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- property binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="property-binding">Property Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion property-binding-1 --> | 
					
						
							|  |  |  | <img [src]="heroImageUrl"> | 
					
						
							|  |  |  | <!-- #enddocregion property-binding-1 --> | 
					
						
							|  |  |  | <!-- #docregion property-binding-2 --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <button [disabled]="isUnchanged">Cancel is disabled</button> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion property-binding-2 --> | 
					
						
							|  |  |  | <!-- #docregion property-binding-3 --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <div [ngClass]="classes">[ngClass] binding to the classes property</div> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion property-binding-3 --> | 
					
						
							|  |  |  | <!-- #docregion property-binding-4 --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <hero-detail [hero]="currentHero"></hero-detail> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion property-binding-4 --> | 
					
						
							|  |  |  | <!-- #docregion property-binding-5 --> | 
					
						
							|  |  |  | <img bind-src="heroImageUrl"> | 
					
						
							|  |  |  | <!-- #enddocregion property-binding-5 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion property-binding-6 --> | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |   <!-- BAD! HeroDetailComponent.hero expects a Hero object,
 | 
					
						
							|  |  |  |   not the string "currentHero". | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |   <hero-detail hero="currentHero"></hero-detail> --> | 
					
						
							|  |  |  | <!-- #enddocregion property-binding-6 --> | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  | <!--  In checked mode, uncommenting the hero-detail above causes this:
 | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  |     EXCEPTION: type 'String' is not a subtype of type 'Hero' of 'value'. --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <!-- #docregion property-binding-7 --> | 
					
						
							|  |  |  | <hero-detail prefix="You are my" [hero]="currentHero"></hero-detail> | 
					
						
							|  |  |  | <!-- #enddocregion property-binding-7 --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion property-binding-vs-interpolation --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | Interpolated: <img src="{{heroImageUrl}}"><br> | 
					
						
							|  |  |  | Property bound: <img [src]="heroImageUrl"> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <div>The interpolated title is {{title}}</div> | 
					
						
							|  |  |  | <div [textContent]="'The [textContent] title is '+title"></div> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion property-binding-vs-interpolation --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- attribute binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="attribute-binding">Attribute Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!--  create and set a colspan attribute --> | 
					
						
							|  |  |  | <!-- #docregion attrib-binding-colspan --> | 
					
						
							|  |  |  | <table border=1> | 
					
						
							|  |  |  |   <!--  expression calculates colspan=2 --> | 
					
						
							|  |  |  |   <tr><td [attr.colspan]="1 + 1">One-Two</td></tr> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   <!-- ERROR: There is no `colspan` property to set!
 | 
					
						
							|  |  |  |     <tr><td colspan="{{1 + 1}}">Three-Four</td></tr> | 
					
						
							|  |  |  |   --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   <tr><td>Five</td><td>Six</td></tr> | 
					
						
							|  |  |  | </table> | 
					
						
							|  |  |  | <!-- #enddocregion attrib-binding-colspan --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <!-- #docregion attrib-binding-aria --> | 
					
						
							|  |  |  | <!-- create and set an aria attribute for assistive technology --> | 
					
						
							|  |  |  | <button [attr.aria-label]="actionName">{{actionName}} with Aria</button> | 
					
						
							|  |  |  | <!-- #enddocregion attrib-binding-aria --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- The following effects are not discussed in the chapter --> | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- any use of [attr.disabled] creates the disabled attribute --> | 
					
						
							|  |  |  |   <button [attr.disabled]="isUnchanged">Disabled</button> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   <button [attr.disabled]="!isUnchanged">Disabled as well</button> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   <!-- can't remove it with [attr.disabled] either --> | 
					
						
							|  |  |  |   <button disabled [attr.disabled]>Still disabled</button> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   <!-- we'd have to remove it with property binding --> | 
					
						
							|  |  |  |   <button disabled [disabled]="false">Enabled (but inert)</button> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- class binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="class-binding">Class Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion class-binding-1 --> | 
					
						
							|  |  |  | <!-- standard class attribute setting  --> | 
					
						
							|  |  |  | <div class="bad curly special">Bad curly special</div> | 
					
						
							|  |  |  | <!-- #enddocregion class-binding-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion class-binding-2 --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <!-- reset/override all class names with a binding  --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <div class="bad curly special" | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |      [class]="badCurly">Bad curly</div> | 
					
						
							|  |  |  |      <!-- Doesn't work, due to github.com/angular/angular/issues/6901 --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion class-binding-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion class-binding-3 --> | 
					
						
							|  |  |  | <!-- #docregion class-binding-3a --> | 
					
						
							|  |  |  | <!-- toggle the "special" class on/off with a property --> | 
					
						
							|  |  |  | <div [class.special]="isSpecial">The class binding is special</div> | 
					
						
							|  |  |  | <!-- #enddocregion class-binding-3a --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- binding to `class.special` trumps the class attribute --> | 
					
						
							|  |  |  | <div class="special" | 
					
						
							|  |  |  |      [class.special]="!isSpecial">This one is not so special</div> | 
					
						
							|  |  |  | <!-- #enddocregion class-binding-3 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div bind-class.special="isSpecial">This class binding is special too</div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!--style binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="style-binding">Style Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion style-binding-1 --> | 
					
						
							|  |  |  | <button [style.color] = "isSpecial ? 'red': 'green'">Red</button> | 
					
						
							|  |  |  | <button [style.background-color]="canSave ? 'cyan': 'grey'" >Save</button> | 
					
						
							|  |  |  | <!-- #enddocregion style-binding-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion style-binding-2 --> | 
					
						
							|  |  |  | <button [style.font-size.em]="isSpecial ? 3 : 1" >Big</button> | 
					
						
							|  |  |  | <button [style.font-size.%]="!isSpecial ? 150 : 50" >Small</button> | 
					
						
							|  |  |  | <!-- #enddocregion style-binding-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- event binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="event-binding">Event Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion event-binding-1 --> | 
					
						
							|  |  |  | <button (click)="onSave()">Save</button> | 
					
						
							|  |  |  | <!-- #enddocregion event-binding-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion event-binding-2 --> | 
					
						
							|  |  |  | <button on-click="onSave()">On Save</button> | 
					
						
							|  |  |  | <!-- #enddocregion event-binding-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  | <!-- #docregion event-binding-3 --> | 
					
						
							|  |  |  | <!-- `myClick` is an event on the custom `MyClickDirective` --> | 
					
						
							|  |  |  | <!-- #docregion my-click --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <div (myClick)="clickMessage=$event">click with myClick</div> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion my-click --> | 
					
						
							|  |  |  | <!-- #enddocregion event-binding-3 --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | {{clickMessage}} | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- binding to a nested component --> | 
					
						
							|  |  |  | <!-- #docregion event-binding-to-component --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <hero-detail (deleteRequest)="deleteHero($event)" [hero]="currentHero"></hero-detail> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion event-binding-to-component --> | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <big-hero-detail | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  |     (deleteRequest)="deleteHero($event)" | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |     [hero]="currentHero"> | 
					
						
							|  |  |  | </big-hero-detail> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion event-binding-bubbling --> | 
					
						
							|  |  |  | <div class="parent-div" (click)="onClickMe($event)">Click me | 
					
						
							|  |  |  |   <div class="child-div">Click me too!</div> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <!-- #enddocregion event-binding-bubbling --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion event-binding-no-propagation --> | 
					
						
							|  |  |  | <!-- Will save only once --> | 
					
						
							|  |  |  | <div (click)="onSave()"> | 
					
						
							|  |  |  |   <button (click)="onSave()">Save, no propagation</button> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <!-- #enddocregion event-binding-no-propagation --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | <!-- #docregion event-binding-propagation --> | 
					
						
							|  |  |  | <!-- Will save twice --> | 
					
						
							|  |  |  | <div (click)="onSave()"> | 
					
						
							|  |  |  |   <button (click)="onSave() || true">Save w/ propagation</button> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <!-- #enddocregion event-binding-propagation --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- Two way data binding unwound;
 | 
					
						
							|  |  |  |     passing the changed display value to the event handler via `$event` --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="ngModel">NgModel (two-way) Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <h3>Result: {{currentHero.firstName}}</h3> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion without-NgModel --> | 
					
						
							|  |  |  | <input [value]="currentHero.firstName" | 
					
						
							|  |  |  |        (input)="currentHero.firstName=$event.target.value" > | 
					
						
							|  |  |  | <!-- #enddocregion without-NgModel --> | 
					
						
							|  |  |  | without NgModel | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <!-- #docregion NgModel-1 --> | 
					
						
							|  |  |  | <input [(ngModel)]="currentHero.firstName"> | 
					
						
							|  |  |  | <!-- #enddocregion NgModel-1 --> | 
					
						
							|  |  |  | [(ngModel)] | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <!-- #docregion NgModel-2 --> | 
					
						
							|  |  |  | <input bindon-ngModel="currentHero.firstName"> | 
					
						
							|  |  |  | <!-- #enddocregion NgModel-2 --> | 
					
						
							|  |  |  | bindon-ngModel | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <!-- #docregion NgModel-3 --> | 
					
						
							|  |  |  | <input | 
					
						
							|  |  |  |     [ngModel]="currentHero.firstName" | 
					
						
							|  |  |  |     (ngModelChange)="currentHero.firstName=$event"> | 
					
						
							|  |  |  | <!-- #enddocregion NgModel-3 --> | 
					
						
							|  |  |  | (ngModelChange) = "...firstName=$event" | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | <!-- #docregion NgModel-4 --> | 
					
						
							|  |  |  | <input | 
					
						
							|  |  |  |     [ngModel]="currentHero.firstName" | 
					
						
							|  |  |  |     (ngModelChange)="setUpperCaseFirstName($event)"> | 
					
						
							|  |  |  | <!-- #enddocregion NgModel-4 --> | 
					
						
							|  |  |  | (ngModelChange) = "setUpperCaseFirstName($event)" | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- NgClass binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="ngClass">NgClass Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p>setClasses returns {{setClasses() | json}}</p> | 
					
						
							|  |  |  | <!-- #docregion NgClass-1 --> | 
					
						
							|  |  |  | <div [ngClass]="setClasses()">This div is saveable and special</div> | 
					
						
							|  |  |  | <!-- #enddocregion NgClass-1 --> | 
					
						
							|  |  |  | <div [ngClass]="setClasses()" #classDiv> | 
					
						
							|  |  |  |   After setClasses(), the classes are "{{classDiv.className}}" | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- not used in chapter --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div [ngClass]="isSpecial ? 'special' : ''">This div is special</div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div class="bad curly special">Bad curly special</div> | 
					
						
							|  |  |  | <div [ngClass]="{bad:false, curly:true, special:true}">Curly special</div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- NgStyle binding --> | 
					
						
							|  |  |  | <hr><h2>NgStyle Binding</h2> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion NgStyle-1 --> | 
					
						
							|  |  |  | <div [style.font-size]="isSpecial ? 'x-large' : 'smaller'" > | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  |   This div is x-large. | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | </div> | 
					
						
							|  |  |  | <!-- #enddocregion NgStyle-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h3>Use setStyles() - CSS property names</h3> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <p>setStyles returns {{setStyles()}}.</p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #docregion NgStyle-2 --> | 
					
						
							|  |  |  | <div [ngStyle]="setStyles()"> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  |   This div is italic, normal weight, and extra large (24px). | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | </div> | 
					
						
							|  |  |  | <!-- #enddocregion NgStyle-2 --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <p>After setStyles(), the DOM confirms that the styles are | 
					
						
							|  |  |  |   <span [ngStyle]="setStyles()" #styleDiv> | 
					
						
							|  |  |  |     {{getStyles(styleDiv)}} | 
					
						
							|  |  |  |   </span>. | 
					
						
							|  |  |  | </p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- not used in chapter --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- NgIf binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="ngIf">NgIf Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion NgIf-1 --> | 
					
						
							|  |  |  | <div *ngIf="currentHero != null">Hello, {{currentHero.firstName}}</div> | 
					
						
							|  |  |  | <!-- #enddocregion NgIf-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion NgIf-2 --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <!-- not displayed because nullHero is false.
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |     `nullHero.firstName` never has a chance to fail --> | 
					
						
							|  |  |  | <div *ngIf="nullHero != null">Hello, {{nullHero.firstName}}</div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- Hero Detail is not in the DOM because isActive is false--> | 
					
						
							|  |  |  | <hero-detail *ngIf="isActive"></hero-detail> | 
					
						
							|  |  |  | <!-- #enddocregion NgIf-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- NgIf binding with template (no *) --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <template [ngIf]="currentHero != null">Add {{currentHero.firstName}} with template</template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- Does not show because isActive is false! --> | 
					
						
							|  |  |  | <div>Hero Detail removed from DOM (via template) because isActive is false</div> | 
					
						
							|  |  |  | <template [ngIf]="isActive"> | 
					
						
							|  |  |  |   <hero-detail></hero-detail> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion NgIf-3 --> | 
					
						
							|  |  |  | <!-- isSpecial is true --> | 
					
						
							|  |  |  | <div [class.hidden]="!isSpecial">Show with class</div> | 
					
						
							|  |  |  | <div [class.hidden]="isSpecial">Hide with class</div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- HeroDetail is in the DOM but hidden --> | 
					
						
							|  |  |  | <hero-detail [class.hidden]="isSpecial"></hero-detail> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div [style.display]="isSpecial ? 'block' : 'none'">Show with style</div> | 
					
						
							|  |  |  | <div [style.display]="isSpecial ? 'none'  : 'block'">Hide with style</div> | 
					
						
							|  |  |  | <!-- #enddocregion NgIf-3 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- NgSwitch binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="ngSwitch">NgSwitch Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <fieldset #toePicker (click)="toeChooser(toePicker)" > | 
					
						
							|  |  |  |   <input type="radio" name="toes" value="Eenie">Eenie | 
					
						
							|  |  |  |   <input type="radio" name="toes" value="Meanie">Meanie | 
					
						
							|  |  |  |   <input type="radio" name="toes" value="Miney">Miney | 
					
						
							|  |  |  |   <input type="radio" name="toes" value="Moe">Moe | 
					
						
							|  |  |  |   <input type="radio" name="toes" value="???">??? | 
					
						
							|  |  |  | </fieldset> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div class="toe"> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |     <div *ngIf="toeChoice == null">Pick a toe</div> | 
					
						
							|  |  |  |     <div *ngIf="toeChoice != null"> | 
					
						
							|  |  |  |       You picked ... | 
					
						
							|  |  |  |       <!-- #docregion NgSwitch, NgSwitch-expanded --> | 
					
						
							|  |  |  |       <span [ngSwitch]="toeChoice"> | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |       <!-- #enddocregion NgSwitch --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         <!-- with *NgSwitch --> | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |         <!-- #docregion NgSwitch --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |         <span *ngSwitchWhen="'Eenie'">Eenie</span> | 
					
						
							|  |  |  |         <span *ngSwitchWhen="'Meanie'">Meanie</span> | 
					
						
							|  |  |  |         <span *ngSwitchWhen="'Miney'">Miney</span> | 
					
						
							|  |  |  |         <span *ngSwitchWhen="'Moe'">Moe</span> | 
					
						
							|  |  |  |         <span *ngSwitchDefault>other</span> | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |         <!-- #enddocregion NgSwitch --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         <!-- with <template> --> | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  |         <template ngSwitchWhen="Eenie"><span>Eenie</span></template> | 
					
						
							|  |  |  |         <template ngSwitchWhen="Meanie"><span>Meanie</span></template> | 
					
						
							|  |  |  |         <template ngSwitchWhen="Miney"><span>Miney</span></template> | 
					
						
							|  |  |  |         <template ngSwitchWhen="Moe"><span>Moe</span></template> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |         <template ngSwitchDefault><span>other</span></template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-22 11:43:15 -08:00
										 |  |  |       <!-- #docregion NgSwitch --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |       </span> | 
					
						
							|  |  |  |       <!-- #enddocregion NgSwitch, NgSwitch-expanded --> | 
					
						
							|  |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- NgFor binding --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="ngFor">NgFor Binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <div class="box"> | 
					
						
							|  |  |  |   <!-- #docregion NgFor-1 --> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes">{{hero.fullName}}</div> | 
					
						
							|  |  |  |   <!-- #enddocregion NgFor-1 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div class="box"> | 
					
						
							|  |  |  |   <!-- *ngFor w/ hero-detail Component --> | 
					
						
							|  |  |  |   <!-- #docregion NgFor-2 --> | 
					
						
							|  |  |  |   <hero-detail *ngFor="#hero of heroes" [hero]="hero"></hero-detail> | 
					
						
							|  |  |  |   <!-- #enddocregion NgFor-2 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h4 id="ngFor-index">NgFor with index</h4> | 
					
						
							|  |  |  | <p>with <i>semi-colon</i> separator</p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <div class="box"> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  |   <!-- #docregion NgFor-3 --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |   <div *ngFor="#hero of heroes; #i=index">{{i + 1}} - {{hero.fullName}}</div> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |   <!-- #enddocregion NgFor-3 --> | 
					
						
							|  |  |  | </div> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p>with <i>comma</i> separator</p> | 
					
						
							|  |  |  | <div class="box"> | 
					
						
							|  |  |  |   <!-- Ex: "1 - Hercules Son of Zeus"" --> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes, #i=index">{{i + 1}} - {{hero.fullName}}</div> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h4 id="ngFor-trackBy">NgForTrackBy</h4> | 
					
						
							|  |  |  | <button (click)="refreshHeroes()">Refresh heroes</button> | 
					
						
							|  |  |  | <p>First hero: <input [(ngModel)]="heroes[0].firstName"></p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p><i>without</i> trackBy</p> | 
					
						
							|  |  |  | <div #noTrackBy class="box"> | 
					
						
							|  |  |  |   <!-- #docregion NgForTrackBy-1 --> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes">({{hero.id}}) {{hero.fullName}}</div> | 
					
						
							|  |  |  |   <!-- #enddocregion NgForTrackBy-1 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <div id="noTrackByCnt" *ngIf="heroesNoTrackByChangeCount != 0" style="background-color:bisque"> | 
					
						
							|  |  |  |   Hero DOM elements change #<span style="background-color:gold">{{heroesNoTrackByChangeCount}}</span> without trackBy | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p>with trackBy and <i>semi-colon</i> separator</p> | 
					
						
							|  |  |  | <div #withTrackBy class="box"> | 
					
						
							|  |  |  |   <!-- #docregion NgForTrackBy-2 --> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes; trackBy:trackByHeroes">({{hero.id}}) {{hero.fullName}}</div> | 
					
						
							|  |  |  |   <!-- #enddocregion NgForTrackBy-2 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <div id="withTrackByCnt" *ngIf="heroesWithTrackByChangeCount != 0" style="background-color:bisque"> | 
					
						
							|  |  |  |   Hero DOM elements change #<span style="background-color:gold">{{heroesWithTrackByChangeCount}}</span> with trackBy | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p>with trackBy and <i>comma</i> separator</p> | 
					
						
							|  |  |  | <div class="box"> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes, trackBy:trackByHeroes">({{hero.id}}) {{hero.fullName}}</div> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p>with trackBy and <i>space</i> separator</p> | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  | <div class="box"> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |   <div *ngFor="#hero of heroes trackBy:trackByHeroes">({{hero.id}}) {{hero.fullName}}</div> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p>with <i>*ngForTrackBy</i></p> | 
					
						
							|  |  |  | <div class="box"> | 
					
						
							|  |  |  |   <!-- #docregion NgForTrackBy-2 --> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes" *ngForTrackBy="trackByHeroes">({{hero.id}}) {{hero.fullName}}</div> | 
					
						
							|  |  |  |   <!-- #enddocregion NgForTrackBy-2 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p>with <i>generic</i> trackById function</p> | 
					
						
							|  |  |  | <div class="box"> | 
					
						
							|  |  |  |   <!-- #docregion NgForTrackBy-3 --> | 
					
						
							|  |  |  |   <div *ngFor="#hero of heroes" *ngForTrackBy="trackById">({{hero.id}}) {{hero.fullName}}</div> | 
					
						
							|  |  |  |   <!-- #enddocregion NgForTrackBy-3 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- * and template --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="star-prefix">* prefix and <template></h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <h3>*ngIf expansion</h3> | 
					
						
							|  |  |  | <p><i>*ngIf</i></p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #docregion Template-1 --> | 
					
						
							|  |  |  | <hero-detail *ngIf="currentHero != null" [hero]="currentHero"></hero-detail> | 
					
						
							|  |  |  | <!-- #enddocregion Template-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <p><i>expand to template = "..."</i></p> | 
					
						
							|  |  |  | <!-- #docregion Template-2a --> | 
					
						
							|  |  |  | <hero-detail template="ngIf:currentHero != null" [hero]="currentHero"></hero-detail> | 
					
						
							|  |  |  | <!-- #enddocregion Template-2a --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p><i>expand to <template></i></p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #docregion Template-2 --> | 
					
						
							|  |  |  | <template [ngIf]="currentHero != null"> | 
					
						
							|  |  |  |     <hero-detail [hero]="currentHero"></hero-detail> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | <!-- #enddocregion Template-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <h3>*ngFor expansion</h3> | 
					
						
							|  |  |  | <p><i>*ngFor</i></p> | 
					
						
							|  |  |  |   <!-- *ngFor w/ hero-detail Component --> | 
					
						
							|  |  |  |   <!-- #docregion Template-3a --> | 
					
						
							|  |  |  |   <hero-detail *ngFor="#hero of heroes; trackBy:trackByHeroes" [hero]="hero"></hero-detail> | 
					
						
							|  |  |  |   <!-- #enddocregion Template-3a --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <p><i>expand to template = "..."</i></p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <div class="box"> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |   <!-- *ngFor w/ hero-detail Component and a template "attribute" directive --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |   <!-- #docregion Template-3 --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  |   <hero-detail template="ngFor #hero of heroes; trackBy:trackByHeroes" [hero]="hero"></hero-detail> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |   <!-- #enddocregion Template-3 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | <br> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <p><i>expand to <template></i></p> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <div class="box"> | 
					
						
							|  |  |  |   <!-- ngFor w/ hero-detail Component inside a template element --> | 
					
						
							|  |  |  |   <!-- #docregion Template-4 --> | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  |   <template ngFor #hero [ngForOf]="heroes" [ngForTrackBy]="trackByHeroes"> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  |       <hero-detail [hero]="hero"></hero-detail> | 
					
						
							|  |  |  |   </template> | 
					
						
							|  |  |  |   <!-- #enddocregion Template-4 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- template local variable --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="local-vars">Template local variables</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion var-phone --> | 
					
						
							|  |  |  | <!-- phone refers to the input element; pass its `value` to an event handler --> | 
					
						
							|  |  |  | <input #phone placeholder="phone number"> | 
					
						
							|  |  |  | <button (click)="callPhone(phone.value)">Call</button> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- fax refers to the input element; pass its `value` to an event handler --> | 
					
						
							|  |  |  | <input var-fax placeholder="phone number"> | 
					
						
							|  |  |  | <button (click)="callFax(fax.value)">Fax</button> | 
					
						
							|  |  |  | <!-- #enddocregion var-phone --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <h4>Example Form</h4> | 
					
						
							|  |  |  | <!-- #docregion var-form --> | 
					
						
							|  |  |  | <!-- #docregion var-form-a --> | 
					
						
							|  |  |  | <form (ngSubmit)="onSubmit(theForm)" #theForm="ngForm"> | 
					
						
							|  |  |  | <!-- #enddocregion var-form-a --> | 
					
						
							|  |  |  |   <div class="form-group"> | 
					
						
							|  |  |  |     <label for="name">Name</label> | 
					
						
							|  |  |  |     <input id="name" class="form-control" required ngControl="firstName" | 
					
						
							|  |  |  |         [(ngModel)]="currentHero.firstName"> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  |   <!-- #docregion var-form-a --> | 
					
						
							|  |  |  |   <button type="submit" [disabled]="!theForm.form.valid">Submit</button> | 
					
						
							|  |  |  | </form> | 
					
						
							|  |  |  | <!-- #enddocregion var-form-a --> | 
					
						
							|  |  |  | <!-- #enddocregion var-form --> | 
					
						
							|  |  |  | <br><br> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- btn refers to the button element; show its disabled state --> | 
					
						
							|  |  |  | <button #btn disabled [textContent]="'disabled by attribute: ' + btn.disabled.toString()"></button> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- inputs and output --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="inputs-and-outputs">Inputs and Outputs</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion io-1 --> | 
					
						
							|  |  |  | <img [src]="iconUrl"/> | 
					
						
							|  |  |  | <button (click)="onSave()">Save</button> | 
					
						
							|  |  |  | <!-- #enddocregion io-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion io-2 --> | 
					
						
							| 
									
										
										
										
											2016-02-17 10:59:14 -08:00
										 |  |  | <hero-detail [hero]="currentHero" (deleteRequest)="deleteHero($event)"> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | </hero-detail> | 
					
						
							|  |  |  | <!-- #enddocregion io-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <div (myClick)="clickMessage2=$event">myClick2</div> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | {{clickMessage2}} | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- Pipes --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="pipes">Pipes</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion pipes-1 --> | 
					
						
							|  |  |  | <!-- Force title to uppercase --> | 
					
						
							|  |  |  | <div>{{ title | uppercase }}</div> | 
					
						
							|  |  |  | <!-- #enddocregion pipes-1 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion pipes-2 --> | 
					
						
							|  |  |  | <!-- Pipe chaining: force title to uppercase, then to lowercase --> | 
					
						
							|  |  |  | <div>{{ title | uppercase | lowercase }}</div> | 
					
						
							|  |  |  | <!-- #enddocregion pipes-2 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion pipes-3 --> | 
					
						
							|  |  |  | <!-- pipe with configuration argument => "February 25, 1970" --> | 
					
						
							|  |  |  | <div>Birthdate: {{currentHero?.birthdate | date:'longDate'}}</div> | 
					
						
							|  |  |  | <!-- #enddocregion pipes-3 --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion pipes-json --> | 
					
						
							| 
									
										
										
										
											2016-02-23 14:33:54 +00:00
										 |  |  | <!-- We don't suggest using json for debugging; you'd probably use toString() instead.
 | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  |      Is there a good use for the json pipe in Dart? --> | 
					
						
							|  |  |  | <!--<div>{{currentHero | json}}</div>--> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!-- #enddocregion pipes-json --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div>Birthdate: {{(currentHero?.birthdate | date:'longDate') | uppercase}}</div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- pipe price to USD and display the $ symbol --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  |   <label>Price: </label>{{product['price'] | currency:'$'}} | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- Null values and the Elvis operator --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="elvis">Elvis <i>?.</i></h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion elvis-1 --> | 
					
						
							|  |  |  |   The title is {{ title }} | 
					
						
							|  |  |  |   <!-- #enddocregion elvis-1 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion elvis-2 --> | 
					
						
							|  |  |  |   The current hero's name is {{currentHero?.firstName}} | 
					
						
							|  |  |  |   <!-- #enddocregion elvis-2 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion elvis-3 --> | 
					
						
							|  |  |  |   The current hero's name is {{currentHero.firstName}} | 
					
						
							|  |  |  |   <!-- #enddocregion elvis-3 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | <!--
 | 
					
						
							|  |  |  | The null hero's name is {{nullHero.firstName}} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  |   See console log: | 
					
						
							|  |  |  |   EXCEPTION: The null object does not have a getter 'firstName'. | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion elvis-4 --> | 
					
						
							|  |  |  | <!--No hero, div not displayed, no error --> | 
					
						
							|  |  |  | <div *ngIf="nullHero != null">The null hero's name is {{nullHero.firstName}}</div> | 
					
						
							|  |  |  | <!-- #enddocregion elvis-4 --> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <!-- skip docregion elvis-5 --> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <!-- #docregion elvis-6 --> | 
					
						
							|  |  |  |   <!-- No hero, no problem! --> | 
					
						
							|  |  |  |   The null hero's name is {{nullHero?.firstName}} | 
					
						
							|  |  |  |   <!-- #enddocregion elvis-6 --> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!-- Todo: discuss this in the Style binding section --> | 
					
						
							|  |  |  | <!-- enums in bindings --> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <!--
 | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | <hr><h2 id="enums">Enums in binding</h2> | 
					
						
							| 
									
										
										
										
											2015-11-15 23:38:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | <p>The current color number is {{color}}</p> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | <p><button [style.color]="color.toString()" (click)="colorToggle()">Enum Toggle</button> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <a class="to-toc" href="#toc">top</a> | 
					
						
							| 
									
										
										
										
											2016-01-26 15:48:03 -08:00
										 |  |  | --> | 
					
						
							| 
									
										
										
										
											2016-02-06 17:18:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | <!-- #docregion my-first-app --> | 
					
						
							|  |  |  | <h3>My First Angular Application</h3> | 
					
						
							|  |  |  | <!-- #enddocregion my-first-app --> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <a class="to-toc" href="#toc">top</a> |