{"id":206,"date":"2023-01-02T14:42:48","date_gmt":"2023-01-02T22:42:48","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=206"},"modified":"2023-01-02T15:24:57","modified_gmt":"2023-01-02T23:24:57","slug":"exported-directives","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/exported-directives\/","title":{"rendered":"Exported Directives"},"content":{"rendered":"\n<p>Yesterday, we talked about <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/template-reference-variables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Template Reference Variables<\/a>. Today, I want to show you how a directive can be accessed with a Template Reference Variable.<\/p>\n\n\n\n<p>You&#8217;ve probably seen that syntax before:<\/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\">form<\/span> #<span class=\"hljs-attr\">myForm<\/span>=<span class=\"hljs-string\">\"ngForm\"<\/span> (<span class=\"hljs-attr\">ngSubmit<\/span>)=<span class=\"hljs-string\">\"onSubmit(myForm)\"<\/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>That syntax is possible because the <code>NgForm<\/code> directive is <strong>exported <\/strong>using the following syntax (actual <a href=\"https:\/\/github.com\/angular\/angular\/blob\/15.0.4\/packages\/forms\/src\/directives\/ng_form.ts#L95-L102\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular source code here<\/a>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript\"><span class=\"hljs-meta\">@Directive<\/span>({\n   exportAs: <span class=\"hljs-string\">'ngForm'<\/span>\n})<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The above code enables the usage of Template Reference Variables such as <code>#myForm=\"ngForm\"<\/code> . This technique is widely used in Angular forms and component libraries to expose public directive properties (and methods) to your component&#8217;s template. <\/p>\n\n\n\n<p>For instance, we can access <code>myForm.value<\/code> or <code>myForm.valid<\/code> in an expression.<\/p>\n\n\n\n<p> <code>ngModel<\/code> is exported that way, too. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we talked about Template Reference Variables. Today, I want to show you how a directive can be accessed with a Template Reference Variable. You&#8217;ve probably seen that syntax before: That syntax is possible because the NgForm directive is exported using the following syntax (actual Angular source code here): The above code enables the usage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,7],"tags":[],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-directives","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/206","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=206"}],"version-history":[{"count":21,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/206\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}