{"id":2370,"date":"2025-11-05T03:11:59","date_gmt":"2025-11-05T11:11:59","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2370"},"modified":"2025-11-05T03:12:49","modified_gmt":"2025-11-05T11:12:49","slug":"aliasing-content-for-projection","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/aliasing-content-for-projection\/","title":{"rendered":"Aliasing content for projection"},"content":{"rendered":"\n<p>We have <span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">discussed\u00a0<a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/passing-custom-content-to-a-component-with-content-projection\/\" target=\"_blank\">content projection<\/a>\u00a0before, as well as\u00a0<a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-pass-multiple-pieces-of-content-from-one-component-to-another\/\" target=\"_blank\">multi-slot content projection<\/a>, a t<\/span>echnique where we can pass multiple\u00a0pieces of content from one component to another.<\/p>\n\n\n\n<p>For instance, this can be useful when we want to customize a header, body, and footer of a card or a modal component. We need several different templates to pass to that component, and multi-slot content projection is ideal for this purpose (see an <a href=\"https:\/\/stackblitz.com\/edit\/at-multi-slot-content-projection?file=src%2Fapp%2Fapp.component.html\" target=\"_blank\" rel=\"noopener\" title=\"\">example in action on Stackblitz here<\/a>).<\/p>\n\n\n\n<p>The only downside of such content projection is that it relies on selectors, which can be CSS classes, HTML tag names, or other similar elements. While that works, it&#8217;s not a great developer experience as it does not show explicitely when a piece of HTML is meant to be projected.<\/p>\n\n\n\n<p>This is where aliases come into play. If we have the following child component code, expecting two different pieces of projected content:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"298\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-1-1024x298.png\" alt=\"\" class=\"wp-image-2372\" style=\"width:548px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-1-1024x298.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-1-300x87.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-1-768x224.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-1-1536x447.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-1.png 1676w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>With aliases, we can now project anything into those slots by using the attribute <code>ngProjectAs<\/code> in the parent component:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"642\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-2-1024x642.png\" alt=\"\" class=\"wp-image-2373\" style=\"width:477px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-2-1024x642.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-2-300x188.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-2-768x481.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2025\/11\/carbon-2.png 1136w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>That way, we no longer rely on CSS selectors. We make it explicit that an element is meant to be projected in a given slot. This results in more robust code as it can&#8217;t break when CSS or HTML structures change.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have discussed\u00a0content projection\u00a0before, as well as\u00a0multi-slot content projection, a technique where we can pass multiple\u00a0pieces of content from one component to another. For instance, this can be useful when we want to customize a header, body, and footer of a card or a modal component. We need several different templates to pass to that [&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,21,8],"tags":[],"class_list":["post-2370","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-components"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2370","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=2370"}],"version-history":[{"count":1,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":2374,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2370\/revisions\/2374"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}