1
1
# Template statements
2
2
3
- A template ** statement ** responds to an ** event ** raised by a binding target
4
- such as an element, component, or directive .
3
+ Template statements are methods or properties that you can use in your HTML to respond to user events.
4
+ With template statements, your application can engage users through actions such as displaying dynamic content or submitting forms .
5
5
6
6
<div class =" alert is-helpful " >
7
7
@@ -10,56 +10,63 @@ the syntax and code snippets in this guide.
10
10
11
11
</div >
12
12
13
- The following template statement appears in quotes to the right of the ` = `   ; symbol as in ` (event)="statement" ` .
13
+ In the following example, the template statement ` deleteHero() ` appears in quotes to the right of the ` = `   ; symbol as in ` (event)="statement" ` .
14
14
15
15
<code-example path =" template-syntax/src/app/app.component.html " region =" context-component-statement " header =" src/app/app.component.html " ></code-example >
16
16
17
- A template statement * has a side effect* .
18
- That's the whole point of an event.
19
- It's how you update application state from user action.
17
+ When the user clicks the ** Delete hero** button, Angular calls the ` deleteHero() ` function in the component class.
20
18
21
- Responding to events is the other side of Angular's "unidirectional data flow".
22
- You're free to change anything, anywhere, during this turn of the event loop.
19
+ You can use template statements with elements, components, or directives in response to events.
23
20
24
- Like template expressions, template * statements* use a language that looks like JavaScript.
25
- The template statement parser differs from the template expression parser and
26
- specifically supports both basic assignment (` = ` ) and chaining expressions with <code >;</code >.
21
+ <div class =" alert is-helpful " >
22
+
23
+ Responding to events is an aspect of Angular's [ unidirectional data flow] ( guide/glossary#unidirectional-data-flow ) .
24
+ You can change anything in your application during a single event loop.
25
+
26
+ </div >
27
+
28
+ ## Syntax
27
29
28
- However, certain JavaScript and template expression syntax is not allowed:
30
+ Like [ template expressions] ( guide/interpolation ) , template statements use a language that looks like JavaScript.
31
+ However, the parser for template statements differs from the parser for template expressions.
32
+ In addition, the template statements parser specifically supports both basic assignment, ` = ` , and chaining expressions with semicolons, ` ; ` .
29
33
30
- * <code >new</code >
34
+ The following JavaScript and template expression syntax is not allowed:
35
+
36
+ * ` new `
31
37
* increment and decrement operators, ` ++ ` and ` -- `
32
38
* operator assignment, such as ` += ` and ` -= `
33
39
* the bitwise operators, such as ` | ` and ` & `
34
40
* the [ pipe operator] ( guide/template-expression-operators#pipe )
35
41
36
42
## Statement context
37
43
38
- As with expressions, statements can refer only to what's in the statement context
39
- such as an event handling method of the component instance.
44
+ Statements have a context&mdash ; a particular part of the application to which the statement belongs.
40
45
41
- The * statement context* is typically the component instance.
42
- The * deleteHero* in ` (click)="deleteHero()" ` is a method of the data-bound component.
46
+ Statements can refer only to what's in the statement context, which is typically the component instance.
47
+ For example, ` deleteHero() ` of ` (click)="deleteHero()" ` is a method of the component in the following snippet .
43
48
44
49
<code-example path =" template-syntax/src/app/app.component.html " region =" context-component-statement " header =" src/app/app.component.html " ></code-example >
45
50
46
51
The statement context may also refer to properties of the template's own context.
47
- In the following examples, the template ` $event ` object,
48
- a [ template input variable] ( guide/built-in-directives#template-input-variable ) (` let hero ` ),
49
- and a [ template reference variable] ( guide/template-reference-variables ) (` #heroForm ` )
50
- are passed to an event handling method of the component.
52
+ In the following example, the component's event handling method, ` onSave() ` takes the template's own ` $event ` object as an argument.
53
+ On the next two lines, the ` deleteHero() ` method takes a [ template input variable] ( guide/built-in-directives#template-input-variable ) , ` hero ` , and ` onSubmit() ` takes a [ template reference variable] ( guide/template-reference-variables ) , ` #heroForm ` .
51
54
52
55
<code-example path =" template-syntax/src/app/app.component.html " region =" context-var-statement " header =" src/app/app.component.html " ></code-example >
53
56
57
+ In this example, the context of the ` $event ` object, ` hero ` , and ` #heroForm ` is the template.
58
+
54
59
Template context names take precedence over component context names.
55
- In ` deleteHero(hero) ` above, the ` hero ` is the template input variable,
56
- not the component's ` hero ` property.
60
+ In the preceding ` deleteHero(hero) ` , the ` hero ` is the template input variable, not the component's ` hero ` property.
61
+
62
+ ## Statement best practices
63
+
64
+ * ** Conciseness**
57
65
58
- ## Statement guidelines
66
+ Keep template statements minimal by using method calls or basic property assignments.
59
67
60
- Template statements cannot refer to anything in the global namespace. They
61
- can't refer to ` window ` or ` document ` .
62
- They can't call ` console.log ` or ` Math.max ` .
68
+ * ** Work within the context**
63
69
64
- As with expressions, avoid writing complex template statements.
65
- A method call or simple property assignment should be the norm.
70
+ The context of a template statement can be the component class instance or the template.
71
+ Because of this, template statements cannot refer to anything in the global namespace such as ` window ` or ` document ` .
72
+ For example, template statements can't call ` console.log() ` or ` Math.max() ` .
0 commit comments