{"flag":true,"single":true,"pageTitle":"Template Reference Variable | Data Binding | Angular 15+","post":{"id":61,"user_id":"1","slug":"template-reference-variable-data-binding-angular-15--hlpv","title":"Template Reference Variable | Data Binding | Angular 15+","body":"<p>A Template reference variable is a reference to any DOM element, component or a directive in the template.&nbsp;It is a special kind of variable that you can declare in a template using the # symbol.<\/p>\r\n<p><strong>demo.component.html<\/strong><\/p>\r\n<p>when input changed, its value print in &lt;p&gt;<\/p>\r\n<pre class=\"language-markup\"><code>&lt;input type=\"text\" #myVariable (keyup)=\"0\"&gt;\r\n&lt;p&gt;{{ myVariable.value }}&lt;\/p&gt;<\/code><\/pre>\r\n<p><strong>use in Elements:<\/strong><\/p>\r\n<p>You can use a template reference variable to reference an element in the template and access its properties or methods. For example, if you have an input element, you can define a reference variable for it and use it to access its value:<\/p>\r\n<p><strong>user.component.html<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;input type=\"text\" #myInput&gt;\r\n&lt;button (click)=\"submit(myInput.value)\"&gt;Submit&lt;\/button&gt;<\/code><\/pre>\r\n<p><strong>user.component.js<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>export class UserComponent {\r\n  submit(val:string){\r\n    console.log(val);\r\n  }\r\n}<\/code><\/pre>\r\n<p><strong>Components:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;app-child #myChild&gt;&lt;\/app-child&gt;\r\n&lt;button (click)=\"myChild.doSomething()\"&gt;Do something&lt;\/button&gt;<\/code><\/pre>\r\n<p>In this example, we have defined a template reference variable <strong>myChild <\/strong>for a child component <strong>app-child<\/strong>. When the button is clicked, we are calling a method <strong>doSomething()<\/strong> on the child component using <strong>myChild.doSomething().<\/strong><\/p>\r\n<p><strong>Directives:<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;div myDirective #myDiv&gt;&lt;\/div&gt;\r\n&lt;button (click)=\"myDiv.myDirectiveMethod()\"&gt;Call directive method&lt;\/button&gt;<\/code><\/pre>\r\n<p>In this example, we have defined a template reference variable myDiv for a div element with the myDirective directive. When the button is clicked, we are calling a method myDirectiveMethod() on the directive using myDiv.myDirectiveMethod().<\/p>","category_id":"13","is_private":"0","created_at":"2023-03-28T00:04:37.000000Z","updated_at":"2023-03-31T05:00:20.000000Z","category":{"id":13,"user_id":"1","name":"Angular","slug":"angular-rfjp","parent_id":"12","created_at":"2023-03-24T03:25:15.000000Z","updated_at":"2023-03-24T03:25:15.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"A Template reference variable is a reference to any DOM element, component or a directive in the template.&nbsp;It is a special kind of vari - Template Reference Variable | Data Binding | Angular 15+ (Updated: March 31, 2023) - Read more about Template Reference Variable | Data Binding | Angular 15+ at my programming site [SITE]","categories":[]}