{"id":26503,"date":"2024-03-27T10:21:36","date_gmt":"2024-03-27T09:21:36","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/?p=26503"},"modified":"2024-04-03T23:43:34","modified_gmt":"2024-04-03T21:43:34","slug":"angular-hostattributetoken-the-new-way-to-inject-attributes","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/","title":{"rendered":"Angular HostAttributeToken: the new way to inject attributes"},"content":{"rendered":"\n<p>Over the recent months, <strong>Angular<\/strong> has been evolving rapidly, introducing new features and a ton of improvements, many of these to improve maintainability and performance.<\/p>\n\n\n\n<p>In this article I want to introduce you to the latest addition brought by <strong>Angular v17.3.0<\/strong>: the new <strong>HostAttributeToken<\/strong> class.<\/p>\n\n\n\n<p>This new API allows you to inject host attributes using the <strong>inject( ) function<\/strong>, similar to the functionality of the <strong>@Attribute<\/strong> decorator.<\/p>\n\n\n\n<p>The <strong>@Attribute<\/strong> decorator is perhaps the lesser-known Angular decorators.<br>Therefore, before delving into how the <strong>HostAttributeToken class<\/strong> works, let\u2019s take a moment to review the fundamentals.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-attribute-do-you-really-need-an-nbsp-input\">@Attribute: do you really need an&nbsp;input?<\/h3>\n\n\n\n<p>Let\u2019s start with a simple scenario in which you want to develop a component requiring a <strong>configurable property<\/strong>.<\/p>\n\n\n\n<p>Specifically, you want to provide this property as a <strong>constant string literal<\/strong>, because it will not change throughout runtime:<\/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 shcb-code-table shcb-line-numbers\"><span class='shcb-loc'><span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">my-divider<\/span> <span class=\"hljs-attr\">size<\/span>=<span class=\"hljs-string\">\"small\"<\/span> \/&gt;<\/span>\n<\/span><\/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>What you would normally do is to create an <strong>input<\/strong> for that property:<\/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 shcb-code-table shcb-line-numbers\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">import<\/span> { Component, Input } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@angular\/core'<\/span>;\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">type<\/span> DividerSize = <span class=\"hljs-string\">'small'<\/span> | <span class=\"hljs-string\">'big'<\/span>;\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-meta\">@Component<\/span>({\n<\/span><\/span><span class='shcb-loc'><span>  selector: <span class=\"hljs-string\">'my-divider'<\/span>,\n<\/span><\/span><mark class='shcb-loc'><span>  template: <span class=\"hljs-string\">'&lt;hr &#91;class]=\"size\" \/&gt;'<\/span>,\n<\/span><\/mark><span class='shcb-loc'><span>  standalone: <span class=\"hljs-literal\">true<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>})\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">class<\/span> MyDivider {\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-meta\">@Input<\/span>() size: DividerSize;\n<\/span><\/mark><span class='shcb-loc'><span>}\n<\/span><\/span><\/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>Which is fine and works quite well. Perhaps too much\u2026<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter has-mobile-text-align-left\"><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"359\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-1.jpeg\" alt=\"\" class=\"wp-image-26778\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-1.jpeg 639w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-1-300x169.jpeg 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-1-400x225.jpeg 400w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/figure><\/div>\n\n\n<p>By creating an input, you are instructing <strong>Angular<\/strong> to create a binding to that property and check it during each change detection cycle.<\/p>\n\n\n\n<p>That is really excessive, you only need that property to be <strong>checked once<\/strong> during component initialization. \ud83e\udd2f<\/p>\n\n\n\n<p>To make this more performant you can instead inject the <strong>host-element attribute<\/strong> in the <strong>constructor<\/strong> thanks to the <strong>@Attribute<\/strong> decorator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript shcb-code-table shcb-line-numbers\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">import<\/span> { Attribute, Component } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@angular\/core'<\/span>;\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">type<\/span> DividerSize = <span class=\"hljs-string\">'small'<\/span> | <span class=\"hljs-string\">'big'<\/span>;\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-meta\">@Component<\/span>({\n<\/span><\/span><span class='shcb-loc'><span>  selector: <span class=\"hljs-string\">'my-divider'<\/span>,\n<\/span><\/span><mark class='shcb-loc'><span>  template: <span class=\"hljs-string\">'&lt;hr &#91;class]=\"size\" \/&gt;'<\/span>,\n<\/span><\/mark><span class='shcb-loc'><span>  standalone: <span class=\"hljs-literal\">true<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>})\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">class<\/span> MyDivider {\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-keyword\">constructor<\/span>(<span class=\"hljs-params\"><span class=\"hljs-meta\">@Attribute<\/span>(<span class=\"hljs-string\">'size'<\/span>) size: <span class=\"hljs-built_in\">string<\/span><\/span>) {}\n<\/span><\/mark><span class='shcb-loc'><span>}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>Using <strong>@Attribute,<\/strong> Angular will read the value only <strong>once<\/strong> and never again.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center\"><strong>Note:<\/strong> This approach works as well with <strong>Directives<\/strong>, feel free to give it a shot!!!<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p>Now that we have covered the basics, let me introduce you to the main topic of this article: the new <strong>HostAttributeToken<\/strong> class.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"367\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-2.jpeg\" alt=\"\" class=\"wp-image-26779\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-2.jpeg 800w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-2-300x138.jpeg 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-2-768x352.jpeg 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-inject-an-attribute-using-hostattributetoken-class\">Inject an Attribute using HostAttributeToken class<\/h3>\n\n\n\n<p>With the release of Angular 14 there is now a cleaner approach to inject providers without using the constructor class: the <strong>inject<\/strong> function.<\/p>\n\n\n\n<p>Up until now, this <strong>inject function<\/strong> allowed to <strong>inject<\/strong> easily components, directives and pipes, but it was missing a method to inject host attributes.<\/p>\n\n\n\n<p>And that\u2019s precisely why the <strong>HostAttributeToken class<\/strong> was introduced:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-code-table shcb-line-numbers\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">import<\/span> { Component, HostAttributeToken, inject } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@angular\/core'<\/span>;\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">export<\/span> type DividerSize = <span class=\"hljs-string\">'small'<\/span> | <span class=\"hljs-string\">'big'<\/span>;\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>@Component({\n<\/span><\/span><span class='shcb-loc'><span>  <span class=\"hljs-attr\">selector<\/span>: <span class=\"hljs-string\">'my-divider'<\/span>,\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-attr\">template<\/span>: <span class=\"hljs-string\">'&lt;hr &#91;class]=\"size\" \/&gt;'<\/span>,\n<\/span><\/mark><span class='shcb-loc'><span>  <span class=\"hljs-attr\">standalone<\/span>: <span class=\"hljs-literal\">true<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>})\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyDivider<\/span> <\/span>{\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-attr\">size<\/span>: string = inject( <span class=\"hljs-keyword\">new<\/span> HostAttributeToken(<span class=\"hljs-string\">'size'<\/span>) );\n<\/span><\/mark><span class='shcb-loc'><span>}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By injecting an <strong>HostAttributeToken class<\/strong> within the <strong>inject function<\/strong>, you get the corresponding <strong>host-element attribute<\/strong> value.<\/p>\n\n\n\n<p>This new API works similarly to <strong>@Attribute<\/strong>, with a notable difference: instead of returning <strong>null<\/strong> when the attribute is missing, it throws an <strong>error<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image caption-align-center\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"206\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-15.png\" alt=\"\" class=\"wp-image-26777\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-15.png 1200w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-15-300x52.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-15-1024x176.png 1024w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-15-768x132.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption class=\"wp-element-caption\">\ud83d\udd25 Error: NG0201: No provider for HostAttributeToken size found.&nbsp;\ud83d\udd25<\/figcaption><\/figure><\/div>\n\n\n<p>This change was made to align the behavior with other injection tokens.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-thanks-for-reading-so-far-nbsp\">Thanks for reading so far&nbsp;\ud83d\ude4f<\/h3>\n\n\n\n<p>I\u2019d like to have your feedback so please feel free to contact me for any. \ud83d\udc4b<\/p>\n\n\n\n<p>Then, if you really liked it,&nbsp;<strong>share it<\/strong>&nbsp;among your community, tech bros, and whoever you want. \ud83d\udc4b\ud83d\ude01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the recent months, Angular has been evolving rapidly, introducing new features and a ton of improvements, many of these to improve maintainability and performance. In this article I want to introduce you to the latest addition brought by Angular v17.3.0: the new HostAttributeToken class. This new API allows you to inject host attributes using&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\">Read more<\/a><\/p>\n","protected":false},"author":200,"featured_media":26774,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[20],"tags":[],"collections":[],"class_list":{"0":"post-26503","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-languages","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Angular HostAttributeToken: the new way to inject attributes - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"Angular new HostAttributeToken API allows you to inject host attributes using the inject function, similar to the @Attribute decorator.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular HostAttributeToken: the new way to inject attributes\" \/>\n<meta property=\"og:description\" content=\"Angular new HostAttributeToken API allows you to inject host attributes using the inject function, similar to the @Attribute decorator.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-27T09:21:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-03T21:43:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Davide Passafaro\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Davide Passafaro\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\"},\"author\":{\"name\":\"Davide Passafaro\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/79170e2a1bfc41ddeaa10827ce803828\"},\"headline\":\"Angular HostAttributeToken: the new way to inject attributes\",\"datePublished\":\"2024-03-27T09:21:36+00:00\",\"dateModified\":\"2024-04-03T21:43:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\"},\"wordCount\":442,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png\",\"articleSection\":[\"Languages and frameworks\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\",\"name\":\"Angular HostAttributeToken: the new way to inject attributes - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png\",\"datePublished\":\"2024-03-27T09:21:36+00:00\",\"dateModified\":\"2024-04-03T21:43:34+00:00\",\"description\":\"Angular new HostAttributeToken API allows you to inject host attributes using the inject function, similar to the @Attribute decorator.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png\",\"width\":1200,\"height\":675,\"caption\":\"angular token ainject attributes by davide passafaro at codemotion magazine. HostAttributeToken\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Languages and frameworks\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/languages\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Angular HostAttributeToken: the new way to inject attributes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Codemotion.Italy\/\",\"https:\/\/x.com\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/79170e2a1bfc41ddeaa10827ce803828\",\"name\":\"Davide Passafaro\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/Davide-photo-1mb-100x100.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/Davide-photo-1mb-100x100.png\",\"caption\":\"Davide Passafaro\"},\"description\":\"My name is Davide Passafaro and I am Senior Frontend Engineer at Awork. I lead two developer communities in Rome, GDG Roma Citt\u00e0 and Angular Rome, and actively contribute to the tech community as a writer and speaker. When i shut down my computer I like to play board games and archery, not both together till now. I also like escape rooms and memes.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/davide-passafaro\/\"],\"url\":\"https:\/\/www.codemotion.com\/magazine\/author\/davidepassafaro\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Angular HostAttributeToken: the new way to inject attributes - Codemotion Magazine","description":"Angular new HostAttributeToken API allows you to inject host attributes using the inject function, similar to the @Attribute decorator.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/","og_locale":"en_US","og_type":"article","og_title":"Angular HostAttributeToken: the new way to inject attributes","og_description":"Angular new HostAttributeToken API allows you to inject host attributes using the inject function, similar to the @Attribute decorator.","og_url":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2024-03-27T09:21:36+00:00","article_modified_time":"2024-04-03T21:43:34+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png","type":"image\/png"}],"author":"Davide Passafaro","twitter_card":"summary_large_image","twitter_creator":"@CodemotionIT","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Davide Passafaro","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/"},"author":{"name":"Davide Passafaro","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/79170e2a1bfc41ddeaa10827ce803828"},"headline":"Angular HostAttributeToken: the new way to inject attributes","datePublished":"2024-03-27T09:21:36+00:00","dateModified":"2024-04-03T21:43:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/"},"wordCount":442,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png","articleSection":["Languages and frameworks"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/","url":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/","name":"Angular HostAttributeToken: the new way to inject attributes - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png","datePublished":"2024-03-27T09:21:36+00:00","dateModified":"2024-04-03T21:43:34+00:00","description":"Angular new HostAttributeToken API allows you to inject host attributes using the inject function, similar to the @Attribute decorator.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png","width":1200,"height":675,"caption":"angular token ainject attributes by davide passafaro at codemotion magazine. HostAttributeToken"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/languages\/angular-hostattributetoken-the-new-way-to-inject-attributes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Languages and frameworks","item":"https:\/\/www.codemotion.com\/magazine\/languages\/"},{"@type":"ListItem","position":3,"name":"Angular HostAttributeToken: the new way to inject attributes"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/79170e2a1bfc41ddeaa10827ce803828","name":"Davide Passafaro","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/Davide-photo-1mb-100x100.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/Davide-photo-1mb-100x100.png","caption":"Davide Passafaro"},"description":"My name is Davide Passafaro and I am Senior Frontend Engineer at Awork. I lead two developer communities in Rome, GDG Roma Citt\u00e0 and Angular Rome, and actively contribute to the tech community as a writer and speaker. When i shut down my computer I like to play board games and archery, not both together till now. I also like escape rooms and memes.","sameAs":["https:\/\/www.linkedin.com\/in\/davide-passafaro\/"],"url":"https:\/\/www.codemotion.com\/magazine\/author\/davidepassafaro\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-600x400.png","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-600x600.png","author_info":{"display_name":"Davide Passafaro","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/davidepassafaro\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png",1200,675,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-150x150.png",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-300x169.png",300,169,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-768x432.png",768,432,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-1024x576.png",1024,576,true],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png",1200,675,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14.png",1200,675,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-100x100.png",100,100,true],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-180x128.png",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-896x504.png",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-400x225.png",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-600x400.png",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/03\/image-14-600x600.png",600,600,true]},"uagb_author_info":{"display_name":"Davide Passafaro","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/davidepassafaro\/"},"uagb_comment_info":0,"uagb_excerpt":"Over the recent months, Angular has been evolving rapidly, introducing new features and a ton of improvements, many of these to improve maintainability and performance. In this article I want to introduce you to the latest addition brought by Angular v17.3.0: the new HostAttributeToken class. This new API allows you to inject host attributes using&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/26503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/200"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=26503"}],"version-history":[{"count":2,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/26503\/revisions"}],"predecessor-version":[{"id":26930,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/26503\/revisions\/26930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/26774"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=26503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=26503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=26503"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=26503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}