Syntax for Declaring Template Variables
In the template, we use hash symbol (#) to declare the template variable. In the below syntax we’ve declared a template variable called “templateVarName” for the input element. Then the button element is referring to this variable and passes its value to the handleSubmit() method.
<input #templateVarName />
<button type="button" (click)="handleSubmit(templateVarName.value)">Submit</button>
Understanding Templates Variable in Angular
Template variables in Angular allow us to use the data from one part of the template to another part of the same template. There are various use cases where these template variables come in handy. For example, responding to the user input or responding to the form submission.
In this article, we’ll explore what are template variables and how to use them.
Table of Content
- What are Template Variables?
- Syntax for Declaring Template Variables
- Scope of Template Variables
- DOM Element Template Variable
- Using NgForm with template Variables
Contact Us