This section demonstrates in which situations you can access local template reference variables (#ref).
+
+
Accessing in a child template
+
+
+
+
+
+ Value: {{ ref1.value }}
+
+
+
+
+
+
Here's the desugared syntax:
+
+
+
Accessing from outside parent template. (Doesn't work.)
+
+
+
+
+
+
+
+
+
+
+
Here's the desugared syntax:
+
+
+
*ngFor and template reference variable scope
+
+
+
+
+
+
Accessing a on an ng-template
+
+See the console output to see that when you declare the variable on an ng-template, the variable refers to a TemplateRef instance, which represents the template.
+
+
+
+
+
diff --git a/aio/content/examples/template-reference-variables/src/app/app.component.ts b/aio/content/examples/template-reference-variables/src/app/app.component.ts
index ad3a21366c..3ed03c8d82 100644
--- a/aio/content/examples/template-reference-variables/src/app/app.component.ts
+++ b/aio/content/examples/template-reference-variables/src/app/app.component.ts
@@ -8,7 +8,38 @@ import { NgForm } from '@angular/forms';
styleUrls: ['./app.component.css']
})
export class AppComponent {
- @ViewChild('itemForm') form: NgForm;
+
+ public firstExample = 'Hello, World!';
+ public secondExample = 'Hello, World!';
+
+ public ref2 = '';
+
+ public desugared1 = `
+
+
+
+ Value: {{ ref1.value }}
+ `
+ ;
+
+ public desugared2 = `
+
+
+
+
+
+ Value: {{ ref2?.value }}`;
+
+ public ngForExample = `
+
+
+
+
+
+ {{ ref.value }}`;
+
+ @ViewChild('itemForm', { static: false }) form: NgForm;
get submitMessage() { return this._submitMessage; }
private _submitMessage = ''; // tslint:disable-line: variable-name
@@ -25,4 +56,7 @@ export class AppComponent {
console.warn(`Faxing ${value} ...`);
}
+ log(ref3: any) {
+ console.warn(ref3.constructor);
+ }
}
diff --git a/aio/content/guide/template-reference-variables.md b/aio/content/guide/template-reference-variables.md
index 31f2cd140e..a95a0699d5 100644
--- a/aio/content/guide/template-reference-variables.md
+++ b/aio/content/guide/template-reference-variables.md
@@ -1,7 +1,15 @@
-# Template reference variables (`#var`)
+# Template variables
-A **template reference variable** is often a reference to a DOM element within a template.
-It can also refer to a directive (which contains a component), an element, [TemplateRef](api/core/TemplateRef), or a web component.
+Template variables help you use data from one part of a template in another part of the template.
+With template variables, you can perform tasks such as respond to user input or finely tune your application's forms.
+
+A template variable can refer to the following:
+
+* a DOM element within a template
+* a directive
+* an element
+* [TemplateRef](api/core/TemplateRef)
+* a web component
@@ -9,61 +17,138 @@ See the for a working example containing the code
-Use the hash symbol (#) to declare a reference variable.
-The following reference variable, `#phone`, declares a `phone` variable on an `` element.
+## Syntax
+
+In the template, you use the hash symbol, `#`, to declare a template variable.
+The following template variable, `#phone`, declares a `phone` variable on an `` element.
-You can refer to a template reference variable anywhere in the component's template.
+You can refer to a template variable anywhere in the component's template.
Here, a `