{"id":195,"date":"2023-01-02T10:27:57","date_gmt":"2023-01-02T18:27:57","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=195"},"modified":"2023-01-02T15:16:02","modified_gmt":"2023-01-02T23:16:02","slug":"template-reference-variables","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/template-reference-variables\/","title":{"rendered":"Template Reference Variables"},"content":{"rendered":"\n<p>Welcome to the very first edition of my daily Angular Newsletter! As promised, I&#8217;ll keep it nice and short.<\/p>\n\n\n\n<p><br>Today&#8217;s topic is Template Reference variables. I usually call that feature \u201cthe hashtag syntax\u201d because that&#8217;s how it&#8217;s used:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> #<span class=\"hljs-attr\">phone<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\">\"phone number\"<\/span> \/&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here\u2019s how I could use that reference variable to get the value of the input, for instance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> (<span class=\"hljs-attr\">click<\/span>)=<span class=\"hljs-string\">\"callPhone(phone.value)\"<\/span>&gt;<\/span>Call<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code><strong>phone<\/strong><\/code> refers to the input element with the #phone attribute created earlier. That&#8217;s the template reference variable.<br>These variables can be used instead of <strong>ngModel<\/strong>, for instance. Even better, they also work with components and directives!<\/p>\n\n\n\n<p>For instance, the following template reference variable <code>hello<\/code> would have access to all public properties and methods of the <code>HelloComponent<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">app-hello<\/span> #<span class=\"hljs-attr\">hello<\/span> &gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">app-hello<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For more information and examples, you can read another <a href=\"https:\/\/blog.angulartraining.com\/tutorial-the-magic-of-template-reference-variables-3183f0a0d9d1\">short blog post of mine here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the very first edition of my daily Angular Newsletter! As promised, I&#8217;ll keep it nice and short. Today&#8217;s topic is Template Reference variables. I usually call that feature \u201cthe hashtag syntax\u201d because that&#8217;s how it&#8217;s used: Here\u2019s how I could use that reference variable to get the value of the input, for instance: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,7],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":7,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/195\/revisions\/259"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}