2021-11-29 18:18:08 +00:00
# Understanding template variables
2020-04-28 20:26:58 +00:00
2019-06-21 19:31:19 +00:00
Template variables help you use data from one part of a template in another part of the template.
2021-07-21 05:01:39 +00:00
Use template variables to perform tasks such as respond to user input or finely tune your application's forms.
2019-06-21 19:31:19 +00:00
A template variable can refer to the following:
2021-11-29 18:18:08 +00:00
* a DOM element within a template
* a directive or component
* a [TemplateRef ](api/core/TemplateRef ) from an [ng-template ](api/core/ng-template )
* a < a href = "https://developer.mozilla.org/en-US/docs/Web/Web_Components" title = "MDN: Web Components" > web component</ a >
2020-04-28 20:26:58 +00:00
< div class = "alert is-helpful" >
See the < live-example > < / live-example > for a working example containing the code snippets in this guide.
< / div >
2021-11-29 18:18:08 +00:00
## Prerequisites
* [Understanding templates ](guide/template-overview )
2019-06-21 19:31:19 +00:00
## Syntax
2021-11-29 18:18:08 +00:00
In the template, you use the hash symbol, `#` , to declare a template variable.
The following template variable, `#phone` , declares a `phone` variable with the `<input>` element as its value.
2020-04-28 20:26:58 +00:00
2021-11-29 18:18:08 +00:00
< code-example path = "template-reference-variables/src/app/app.component.html" region = "ref-var" header = "src/app/app.component.html" > < / code-example >
2020-04-28 20:26:58 +00:00
2021-07-21 05:01:39 +00:00
Refer to a template variable anywhere in the component's template.
2020-04-28 20:26:58 +00:00
Here, a `<button>` further down the template refers to the `phone` variable.
2021-11-29 18:18:08 +00:00
< code-example path = "template-reference-variables/src/app/app.component.html" region = "ref-phone" header = "src/app/app.component.html" > < / code-example >
2020-04-28 20:26:58 +00:00
2019-06-21 19:31:19 +00:00
## How Angular assigns values to template variables
Angular assigns a template variable a value based on where you declare the variable:
2020-06-09 18:33:02 +00:00
2021-11-29 18:18:08 +00:00
* If you declare the variable on a component, the variable refers to the component instance.
* If you declare the variable on a standard HTML tag, the variable refers to the element.
* If you declare the variable on an `<ng-template>` element, the variable refers to a `TemplateRef` instance which represents the template.
For more information on `<ng-template>` , see [How Angular uses the asterisk, `*`, syntax ](guide/structural-directives#asterisk ) in [Structural directives ](guide/structural-directives ).
## Variable specifying a name
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-11-29 18:18:08 +00:00
* If the variable specifies a name on the right-hand side, such as `#var="ngModel"` , the variable refers to the directive or component on the element with a matching `exportAs` name.
<!-- What does the second half of this mean?^^ Can we explain this more fully? Could I see a working example? - kw -->
2020-06-09 18:33:02 +00:00
2019-06-21 19:31:19 +00:00
### Using `NgForm` with template variables
2020-04-28 20:26:58 +00:00
2019-06-21 19:31:19 +00:00
In most cases, Angular sets the template variable's value to the element on which it occurs.
2020-04-28 20:26:58 +00:00
In the previous example, `phone` refers to the phone number `<input>` .
The button's click handler passes the `<input>` value to the component's `callPhone()` method.
2021-11-29 18:18:08 +00:00
The `NgForm` directive demonstrates getting a reference to a different value by referencing a directive's `exportAs` name.
In the following example, the template variable, `itemForm` , appears three times separated by HTML.
2020-04-28 20:26:58 +00:00
2021-11-29 18:18:08 +00:00
< code-example path = "template-reference-variables/src/app/app.component.html" region = "ngForm" header = "src/app/hero-form.component.html" > < / code-example >
2020-04-28 20:26:58 +00:00
2019-06-21 19:31:19 +00:00
Without the `ngForm` attribute value, the reference value of `itemForm` would be
2022-03-26 22:51:34 +00:00
the [HTMLFormElement ](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement ), `<form>` .
2021-11-29 18:18:08 +00:00
If an element is an Angular Component, a reference with no attribute value will automatically reference the component instance. Otherwise, a reference with no value will reference the DOM element, even if the element has one or more directives applied to it.
<!-- What is the train of thought from talking about a form element to the difference between a component and a directive? Why is the component directive conversation relevant here? - kw I agree - alex -->
2022-03-26 22:51:34 +00:00
2019-06-21 19:31:19 +00:00
## Template variable scope
2021-11-29 18:18:08 +00:00
Just like variables in JavaScript or TypeScript code, template variables are scoped to the template that declares them.
Similarly, [Structural directives ](guide/built-in-directives ) such as `*ngIf` and `*ngFor` , or `<ng-template>` declarations create a new nested template scope, much like JavaScript's control flow statements like `if` and `for` create new lexical scopes. You cannot access template variables within one of these structural directives from outside of its boundaries.
2019-06-21 19:31:19 +00:00
< div class = "alert is-helpful" >
Define a variable only once in the template so the runtime value remains predictable.
< / div >
### Accessing in a nested template
An inner template can access template variables that the outer template defines.
In the following example, changing the text in the `<input>` changes the value in the `<span>` because Angular immediately updates changes through the template variable, `ref1` .
2021-11-29 18:18:08 +00:00
< code-example path = "template-reference-variables/src/app/app.component.html" region = "template-ref-vars-scope1" header = "src/app/app.component.html" > < / code-example >
2019-06-21 19:31:19 +00:00
2021-11-29 18:18:08 +00:00
In this case, the `*ngIf` on `<span>` creates a new template scope, which includes the `ref1` variable from its parent scope.
2019-06-21 19:31:19 +00:00
2021-11-29 18:18:08 +00:00
However, accessing a template variable from a child scope in the parent template doesn't work:
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-11-29 18:18:08 +00:00
```html
< input * ngIf = "true" #ref2 type = "text" [( ngModel )]=" secondExample " />
< span > Value: {{ ref2?.value }}< / span > <!-- doesn't work -->
```
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-11-29 18:18:08 +00:00
Here, `ref2` is declared in the child scope created by `*ngIf` , and is not accessible from the parent template.
2019-06-21 19:31:19 +00:00
2021-11-29 18:18:08 +00:00
{@a template-input-variable}
{@a template-input-variables}
## Template input variable
2020-04-28 20:26:58 +00:00
2023-04-29 18:06:05 +00:00
A _template input variable_ is a variable with a value that is set when an instance of that template is created. See: [Writing structural directives ](/guide/structural-directives )
2020-12-02 17:48:25 +00:00
2021-11-29 18:18:08 +00:00
Template input variables can be seen in action in the long-form usage of `NgFor` :
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-11-29 18:18:08 +00:00
```html
< ul >
< ng-template ngFor let-hero [ ngForOf ] = " heroes " >
2024-02-16 18:58:27 +00:00
< li > {{hero.name}}< / li >
2021-11-29 18:18:08 +00:00
< / ng-template >
< / ul >
```
2020-12-02 17:48:25 +00:00
2021-11-29 18:18:08 +00:00
The `NgFor` directive will instantiate this < ng-template > once for each hero in the `heroes` array, and will set the `hero` variable for each instance accordingly.
2020-12-02 17:48:25 +00:00
2021-11-29 18:18:08 +00:00
When an `<ng-template>` is instantiated, multiple named values can be passed which can be bound to different template input variables. The right-hand side of the `let-` declaration of an input variable can specify which value should be used for that variable.
2020-12-02 17:48:25 +00:00
2021-11-29 18:18:08 +00:00
`NgFor` for example also provides access to the `index` of each hero in the array:
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-11-29 18:18:08 +00:00
```html
< ul >
< ng-template ngFor let-hero let-i = "index" [ ngForOf ] = " heroes " >
2024-02-16 18:58:27 +00:00
< li > Hero number {{i}}: {{hero.name}}< / li >
2021-11-29 18:18:08 +00:00
< / ng-template >
< / ul >
```
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-11-29 18:18:08 +00:00
## What’ s next
2021-08-13 03:32:55 +00:00
2023-04-29 18:06:05 +00:00
[Writing structural directives ](/guide/structural-directives )
2020-12-02 17:48:25 +00:00
2021-11-29 18:18:08 +00:00
@reviewed 2022-05-12