{ "id": "guide/template-statements", "title": "Template statements", "contents": "\n\n\n
Template statements are methods or properties that you can use in your HTML to respond to user events.\nWith template statements, your application can engage users through actions such as displaying dynamic content or submitting forms.
\nSee the
In the following example, the template statement deleteHero()
appears in quotes to the right of the =
symbol as in (event)=\"statement\"
.
When the user clicks the Delete hero button, Angular calls the deleteHero()
method in the component class.
You can use template statements with elements, components, or directives in response to events.
\nResponding to events is an aspect of Angular's unidirectional data flow.\nYou can change anything in your application during a single event loop.
\nLike template expressions, template statements use a language that looks like JavaScript.\nHowever, the parser for template statements differs from the parser for template expressions.\nIn addition, the template statements parser specifically supports both basic assignment, =
, and chaining expressions with semicolons, ;
.
The following JavaScript and template expression syntax is not allowed:
\nnew
++
and --
+=
and -=
|
and &
Statements have a context—a particular part of the application to which the statement belongs.
\nStatements can refer only to what's in the statement context, which is typically the component instance.\nFor example, deleteHero()
of (click)=\"deleteHero()\"
is a method of the component in the following snippet.
The statement context may also refer to properties of the template's own context.\nIn the following example, the component's event handling method, onSave()
takes the template's own $event
object as an argument.\nOn the next two lines, the deleteHero()
method takes a template input variable, hero
, and onSubmit()
takes a template reference variable, #heroForm
.
In this example, the context of the $event
object, hero
, and #heroForm
is the template.
Template context names take precedence over component context names.\nIn the preceding deleteHero(hero)
, the hero
is the template input variable, not the component's hero
property.
Conciseness
\nKeep template statements minimal by using method calls or basic property assignments.
\nWork within the context
\nThe context of a template statement can be the component class instance or the template.\nBecause of this, template statements cannot refer to anything in the global namespace such as window
or document
.\nFor example, template statements can't call console.log()
or Math.max()
.