22
33Templates are markup which is added to HTML to declaratively describe how the application model should be
44projected to DOM as well as which DOM events should invoke which methods on the controller. Templates contain
5- syntax which is core to Angular and allows for data-binding, event-binding, template-instantiation.
5+ syntaxes which are core to Angular and allows for data-binding, event-binding, template-instantiation.
66
77The design of the template syntax has these properties:
88
@@ -35,183 +35,187 @@ detail in the following sections.
3535 <th>Text Interpolation</th>
3636 <td>
3737<pre >
38- ```
39- <div >{{exp}}</div >
40- ```
38+ < ; div> ; {{exp}}< ; /div> ;
4139</pre >
4240
4341Example:
4442<pre >
45- ```
46- <div >
43+ < ; div> ;
4744 Hello {{name}}!
48- < br >
45+ & lt ; br & gt ;
4946 Goodbye {{name}}!
50- </div >
51- ```
47+ < ; /div> ;
5248</pre >
5349 </td>
5450 <td>
55- ` <div [text|index]=exp> `
51+ <pre >
52+ < ; div [text|index]="exp"> ;< ; /div> ;
53+ </pre >
5654
5755Example:
5856<pre >
59- ```
60- <div
57+ < ; div
6158 [text|0]=" 'Hello' + stringify(name) + '!' "
62- [text|2]=" 'Goodbye' + stringify(name) + '!' ">
63- _<b >x</b >_
64- </div >
65- ```
59+ [text|2]=" 'Goodbye' + stringify(name) + '!' "> ;
60+ < ; b> ; x< ; /b> ;
61+ < ; /div> ;
6662</pre >
6763 </td>
6864 </tr>
6965 <tr>
7066 <th>Property Interpolation</th>
7167 <td>
7268<pre >
73- ```
74- <div name =" {{exp}} " >
75- ```
69+ < ; div name="{{exp}}"> ;< ; /div> ;
7670</pre >
7771
7872Example:
7973
8074<pre >
81- ```
82- <div class =" {{selected}} " >`
83- ```
75+ < ; div class="{{selected}}"> ;< ; /div> ;
8476</pre >
8577 </td>
8678 <td>
8779<pre >
88- ```
89- <div [name] =" stringify(exp) " >
90- ```
80+ < ; div [name]="stringify(exp)"> ;< ; /div> ;
9181</pre >
9282
9383Example:
9484
9585<pre >
96- ```
97- <div [title] =" stringify(selected) " >
98- ```
86+ < ; div [title]="stringify(selected)"> ;< ; /div> ;
9987</pre >
10088 </td>
10189 </tr>
10290 <tr>
10391 <th>Property binding</th>
10492 <td>
105- ` <div [prop]="exp"> `
93+ <pre >
94+ < ; div [prop]="exp"> ;< ; /div> ;
95+ </pre >
10696
10797Example:
10898
109- ` <div [hidden]="true"> `
99+ <pre >
100+ < ; div [hidden]="true"> ;< ; /div> ;
101+ </pre >
110102 </td>
111103 <td>
112- ` <div bind-prop="exp"> `
104+ <pre >
105+ < ; div bind-prop="exp"> ;< ; /div> ;
106+ </pre >
113107
114108Example:
115109
116- ` <div bind-hidden="true"> `
110+ <pre >
111+ < ; div bind-hidden="true"> ;< ; /div> ;
112+ </pre >
117113 </td>
118114 </tr>
119115 <tr>
120116 <th>Event binding (non-bubbling)</th>
121117 <td>
122- ` <div (event)="statement"> `
118+ <pre >
119+ < ; div (event)="statement"> ;< ; /div> ;
120+ </pre >
123121
124122Example:
125123
126- ` <div (click)="doX()"> `
124+ <pre >
125+ < ; div (click)="doX()"> ;< ; /div> ;
126+ </pre >
127127 </td>
128128 <td>
129- ` <div on-event="statement"> `
129+ <pre >
130+ < ; div on-event="statement"> ;< ; /div> ;
131+ </pre >
130132
131133Example:
132134
133135<pre >
134- ```
135- <video #player >
136- <button (click) =" player.play() " >play</button >
137- ```
136+ < ; video #player> ;
137+ < ; button (click)="player.play()"> ; play< ; /button> ;
138+ < ; /video> ;
139+ </pre >
140+
141+ Or:
142+
143+ <pre >
144+ < ; div def="symbol"> ;< ; /div> ;
138145</pre >
139- </td>
140- <td>
141- ` <div def="symbol"> `
142146
143147Example:
144148
145149<pre >
146- ```
147- <video def =" player " >
148- <button on-click =" player.play() " >play</button >
149- ```
150+ < ; video def="player"> ;
151+ < ; button on-click="player.play()"> ; play< ; /button> ;
152+ < ; /video> ;
150153</pre >
151154 </td>
152155 </tr>
153156 <tr>
154157 <th>Inline Template</th>
155158 <td>
156- ` <div template="...">...</div> `
159+ <pre >
160+ < ; div template="..."> ; ...< ; /div> ;
161+ </pre >
157162
158163Example:
159164
160165<pre >
161- ```
162- <ul >
163- <li template =" for: #item of items " >
166+ < ; ul> ;
167+ < ; li template="for: #item of items"> ;
164168 {{item}}
165- </li >
166- </ul >
167- ```
169+ < ; /li> ;
170+ < ; /ul> ;
168171</pre >
169172 </td>
170173 <td>
171- ` <template>...</template> `
174+ <pre >
175+ < ; template> ; ...< ; /template> ;
176+ </pre >
172177
173178Example:
174179<pre >
175- ```
176- <ul >
177- <template def-for:"item"
178- bind-for-in="items">
179- <li >
180+ < ; ul> ;
181+ < ; template def-for:"item"
182+ bind-for-in="items"> ;
183+ < ; li> ;
180184 {{item}}
181- </li >
182- </template >
183- </ul >
184- ```
185+ < ; /li> ;
186+ < ; /template> ;
187+ < ; /ul> ;
185188</pre >
186189 </td>
187190 </tr>
188191 <tr>
189192 <th>Explicit Template</th>
190193 <td>
191- ` <template>...</template> `
194+ <pre >
195+ < ; template> ; ...< ; /template> ;
196+ </pre >
192197
193198Example:
194199
195200<pre >
196- ```
197- <template #for="item"
198- [for-in]="items">
201+ < ; template #for="item"
202+ [for-in]="items"> ;
199203 _some_content_to_repeat_
200- </template >
201- ```
204+ < ; /template> ;
202205</pre >
203206 </td>
204207 <td>
205- ` <template>...</template> `
208+ <pre >
209+ < ; template> ; ...< ; /template> ;
210+ </pre >
206211
207212Example:
213+
208214<pre >
209- ```
210- <template def-for="item"
211- bind-for-in="items">
215+ < ; template def-for="item"
216+ bind-for-in="items"> ;
212217 _some_content_to_repeat_
213- </template >
214- ```
218+ < ; /template> ;
215219</pre >
216220 </td>
217221 </tr>
@@ -228,11 +232,11 @@ are always in the form of `property-name` which is assigned an `expression`. The
228232<table >
229233 <tr >
230234 <th>Short form</th>
231- <td>`< some-element [some-property]="expression">` </td>
235+ <td><pre>< some-element [some-property]="expression"></pre> </td>
232236 </tr >
233237 <tr >
234238 <th>Canonical form</th>
235- <td>`< some-element bind-some-property="expression">` </td>
239+ <td><pre>< some-element bind-some-property="expression"></pre> </td>
236240 </tr >
237241</table >
238242
@@ -255,7 +259,7 @@ Key points:
255259* The binding is to the element property not the element attribute.
256260* To prevent custom element from accidentally reading the literal ` expression ` on the title element, the attribute name
257261 is escaped. In our case the ` title ` is escaped to ` [title] ` through the addition of square brackets ` [] ` .
258- * A binding value (in this case ` user.firstName ` will always be an expression, never a string literal) .
262+ * A binding value (in this case ` user.firstName ` ) will always be an expression, never a string literal.
259263
260264NOTE: Unlike Angular v1, Angular v2 binds to properties of elements rather than attributes of elements. This is
261265done to better support custom elements, and to allow binding for values other than strings.
@@ -277,7 +281,7 @@ syntax which is just a short hand for the data binding syntax.
277281is a short hand for:
278282
279283```
280- <span [text|0]=" 'Hello ' + stringify(name) + '!' ">_ </span>
284+ <span [text|0]=" 'Hello ' + stringify(name) + '!' "></span>
281285```
282286
283287The above says to bind the ` 'Hello ' + stringify(name) + '!' ` expression to the zero-th child of the ` span ` 's ` text `
@@ -317,25 +321,25 @@ Views than can be inserted and removed as needed to change the DOM structure.
317321 <tr >
318322 <th>Short form</th>
319323 <td>
320- ```
324+ < pre >
321325parent template
322- < element>
323- < some-element template="instantiating-directive-microsyntax"> child template< /some-element>
324- < /element>
325- ```
326+ & lt ; element& gt ;
327+ & lt ; some-element template="instantiating-directive-microsyntax"& gt ; child template& lt ; /some-element& gt ;
328+ & lt ; /element& gt ;
329+ </ pre >
326330 </td>
327331 </tr >
328332 <tr >
329333 <th>Canonical form</th>
330334 <td>
331- ```
335+ < pre >
332336parent template
333- < element>
334- < template instantiating-directive-bindings>
335- < some-element> child template< /some-element>
336- < /template>
337- < /element>
338- ```
337+ & lt ; element& gt ;
338+ & lt ; template instantiating-directive-bindings& gt ;
339+ & lt ; some-element& gt ; child template& lt ; /some-element& gt ;
340+ & lt ; /template& gt ;
341+ & lt ; /element& gt ;
342+ </ pre >
339343 </td>
340344 </tr >
341345</table >
@@ -448,7 +452,7 @@ microsyntax: ([[key|keyExpression|varExport][;|,]?)*
448452```
449453
450454Where
451- * ` expression ` is an Angular expression as defined in section: Expressions
455+ * ` expression ` is an Angular expression as defined in section: Expressions.
452456* ` local ` is a local identifier for local variables.
453457* ` internal ` is an internal variable which the directive exports for binding.
454458* ` key ` is an attribute name usually only used to trigger a specific directive.
@@ -473,11 +477,11 @@ Binding events allows wiring events from DOM (or other components) to the Angula
473477<table >
474478 <tr >
475479 <th>Short form</th>
476- <td>`< some-element (some-event)="statement">` </td>
480+ <td><pre>< some-element (some-event)="statement"></pre> </td>
477481 </tr >
478482 <tr >
479483 <th>Canonical form</th>
480- <td>`< some-element on-some-event="statement">` </td>
484+ <td><pre>< some-element on-some-event="statement"></pre> </td>
481485 </tr >
482486</table >
483487
@@ -493,11 +497,11 @@ Angular listens to bubbled DOM events (as in the case of clicking on any child),
493497<table >
494498 <tr >
495499 <th>Short form</th>
496- <td>`< some-element (some-event)="statement">` </td>
500+ <td><pre>< some-element (some-event)="statement"></pre> </td>
497501 </tr >
498502 <tr >
499503 <th>Canonical form</th>
500- <td>`< some-element on-some-event="statement">` </td>
504+ <td><pre>< some-element on-some-event="statement"></pre> </td>
501505 </tr >
502506</table >
503507
@@ -519,7 +523,7 @@ component's controller.
519523
520524
521525NOTE: Unlike Angular v1, Angular v2 treats event bindings as core constructs not as directives. This means that there
522- is no need to create a event directive for each kind of event. This makes it possible for Angular v2 to easily
526+ is no need to create an event directive for each kind of event. This makes it possible for Angular v2 to easily
523527bind to custom events of Custom Elements, whose event names are not known ahead of time.
524528
525529
@@ -534,7 +538,7 @@ have different semantics.
534538### Expressions
535539
536540Expressions can be used to bind to properties only. Expressions represent how data should be projected to the View.
537- Expressions should not have any side effects and should be idempotent. Examples of where expressions can be used in
541+ Expressions should not have any side effect and should be idempotent. Examples of where expressions can be used in
538542Angular are:
539543```
540544<div title="{{expression}}">{{expression}}</div>
0 commit comments