You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. If you need autocomplete support from within in your templates from the Angular Language Service: A simple solution that worked for my requirement is: I am using Angular version: 12. They both contain the reference to the input HTML Element. JavaScript frameworks like Angular provide great abstractions and useful APIs on top of the native Document Object Model (DOM) that can make your codebase more readable, maintainable, and efficient. Solution. How to declare a variable in a template in Angular. Saving for retirement starting at 68 years old. For eg. The reason Angular 4 changed <template> to <ng-template> is because there is a name conflict between the <template> tag and the html <template> standard tag. You cannot access template variables outside of these boundaries. Angular. You need to be able to access the var the thread is changing. Your email address will not be published. If you want to do this in a way that will work with AOT (Production) compilation, I would recommend using a template reference variable. Note that you must specify an outlet for the template, but it can be a reference to itself. Angular Documentation Definition Template variables help you use data from one part of a template in another part of the template. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. Unlike the native