Template
Last updated
Was this helpful?
Last updated
Was this helpful?
the text between the braces is a template expression that Angular first evaluates and then converts to a string. Finally, it assigns this composite interpolated result to an element or directive property.
Further info see
[R]
Many JavaScript expressions are legal template expressions, but not all. JavaScript expressions that have or promote side effects are prohibited, including:
assignments (=
, +=
, -=
, ...)
new chaining expressions with ;
or ,
increment and decrement operators (++
and --
)
differences from JavaScript syntax include:
bitwise operators |
and &
new template expression operators
pipe operator |
safe navigation operator ?.
non-null assertion operator!.
Pipe operator
Safe navigation operator
You can also write as following, but is cumbersome especially if the property path is long. e.g.
a.b.c.d
It works perfectly with long property paths such as a?.b?.c?.d
Non-null assertion operator
When the Angular compiler turns your template into TypeScript code, it prevents TypeScript from reporting that hero.name might be null or undefined. Unlike the safe navigation operator, the non-null assertion operator does not guard against null or undefined. Rather it tells the TypeScript type checker to suspend strict null checks for a specific property expression.You'll need this template operator when you turn on strict null checks. It's optional otherwise.
Template expressions cannot refer to anything in the global namespace (except undefined). They can't refer to window
or document
. They can't call console.log
or Math.max
. They are restricted to referencing members of the expression context.
No visible side effects
A template expression should not change any application state other than the value of the target property.
Quick execution
Expressions should finish quickly or the user experience may drag
Simplicity
A property name or method call should be the norm. An occasional Boolean negation (!
) is OK.
Idempotence
an idempotent expression always returns exactly the same thing until one of its dependent values changes.
A template statement responds to an event raised by a binding target such as an element, component, or directive.
supports both basic assignment (=
) and chaining expressions (with ;
or ,
).
certain JavaScript syntax is not allowed (allowed in template expression)
the bitwise operators |
and &
The statement context is typically the component instance. The statement context may also refer to properties of the template's own context.
Template statements cannot refer to anything in the global namespace. They can't refer towindow
ordocument
. They can't callconsole.log
orMath.max
.
Support javascript syntax
new
increment and decrement operators, ++
and --
operator assignment, such as +=
and -=
You declare a template input variable using the let keyword (let hero). The variable's scope is limited to a single instance of the repeated template. You can use the same variable name again in the definition of other structural directives.
If you reference a name that belongs to more than one of these namespaces, the template input variable name takes precedence, followed by a name in the directive's context, and, lastly, the component's member names.
Result
You declare a template reference variable by prefixing the variable name with #
(#var).
Could be a reference to
a DOM element within a template
an Angular component
directive
a web component
The scope of a reference variable is the entire template. Do not define the same variable name more than once in the same template.
In most cases, Angular sets the reference variable's value to the element on which it was declared.
The following is a simplified version of the form example in the Forms guide:
The native <form> element doesn't have a form property. But the NgForm directive does.
The heroForm
is actually a reference to an Angular NgForm directive with the ability to track the value and validity of every control in the form.
The backtick ````` allows you to compose a string over several lines, which makes the HTML more readable.
[R]
[R]
the
Template context names precedence rule :
[R]
Before you imported the, #heroForm
would be the . After Angular take it over, theheroForm
is actually a reference to an Angular directive with the ability to track the value and validity of every control in the form.