Skip to content

Commit 1b70dc9

Browse files
kapunahelewongalxhub
authored andcommitted
docs: edit template-statements doc (angular#38742)
This commit updates the copy and headers to bring in line with style guide and clarify content. PR Close angular#38742
1 parent 3ba97ab commit 1b70dc9

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed
+36-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Template statements
22

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.
55

66
<div class="alert is-helpful">
77

@@ -10,56 +10,63 @@ the syntax and code snippets in this guide.
1010

1111
</div>
1212

13-
The following template statement appears in quotes to the right of the `=`&nbsp;symbol as in `(event)="statement"`.
13+
In the following example, the template statement `deleteHero()` appears in quotes to the right of the `=`&nbsp;symbol as in `(event)="statement"`.
1414

1515
<code-example path="template-syntax/src/app/app.component.html" region="context-component-statement" header="src/app/app.component.html"></code-example>
1616

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.
2018

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.
2320

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
2729

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, `;`.
2933

30-
* <code>new</code>
34+
The following JavaScript and template expression syntax is not allowed:
35+
36+
* `new`
3137
* increment and decrement operators, `++` and `--`
3238
* operator assignment, such as `+=` and `-=`
3339
* the bitwise operators, such as `|` and `&`
3440
* the [pipe operator](guide/template-expression-operators#pipe)
3541

3642
## Statement context
3743

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.
4045

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.
4348

4449
<code-example path="template-syntax/src/app/app.component.html" region="context-component-statement" header="src/app/app.component.html"></code-example>
4550

4651
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`.
5154

5255
<code-example path="template-syntax/src/app/app.component.html" region="context-var-statement" header="src/app/app.component.html"></code-example>
5356

57+
In this example, the context of the `$event` object, `hero`, and `#heroForm` is the template.
58+
5459
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**
5765

58-
## Statement guidelines
66+
Keep template statements minimal by using method calls or basic property assignments.
5967

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**
6369

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

Comments
 (0)