{"id":27610,"date":"2024-05-13T11:04:09","date_gmt":"2024-05-13T09:04:09","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/?p=27610"},"modified":"2024-05-13T11:04:10","modified_gmt":"2024-05-13T09:04:10","slug":"angular-fallback-content-in-ng-content","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/","title":{"rendered":"Angular fallback content in ng-content"},"content":{"rendered":"\n<p><strong>Angular v18<\/strong> recently introduced a new feature to easily define a fallback content in <code><strong>ng-content<\/strong><\/code> elements. In this article, I will discuss about it.<\/p>\n\n\n\n<p>However, before diving into it, I will also explore how it was already possible to achieve the same result in <strong>Angular v17 or earlier versions<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p>When building flexible and reusable UI components, <strong>content projection<\/strong> stands as a fundamental tool at your disposal. It is a pattern in which you insert, or <strong>project<\/strong>, the content you want to use inside another component.<\/p>\n\n\n\n<p>When configuring a component, defining a <strong>fallback content<\/strong> can be useful to ensure something is displayed if no content is provided.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-define-fallback-content-in-ng-content-elements\">Define fallback content in ng-content elements<\/h2>\n\n\n\n<p>Up until <strong>Angular v18<\/strong>, there was not a built-in support to fallback content for <code><strong>ng-content<\/strong><\/code>. However, this didn\u2019t imply there was no workaround.<\/p>\n\n\n\n<p>Let\u2019s take a look at one of the potential implementation strategies:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" 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, ElementRef, computed, viewChild } <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-meta\">@Component<\/span>({\n<\/span><\/span><span class='shcb-loc'><span>  standalone: <span class=\"hljs-literal\">true<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>  selector: <span class=\"hljs-string\">'my-component'<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>  template: <span class=\"hljs-string\">`<\/span>\n<\/span><\/span><mark class='shcb-loc'><span><span class=\"hljs-string\">    @if(!hasContent()){<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span><span class=\"hljs-string\">      &lt;div&gt; Default content &lt;\/div&gt;<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span><span class=\"hljs-string\">    }<\/span>\n<\/span><\/mark><span class='shcb-loc'><span><span class=\"hljs-string\"><\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-string\">    &lt;div #wrapper&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-string\">      &lt;ng-content&gt;&lt;\/ng-content&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-string\">    &lt;\/div&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-string\">  `<\/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> MyComponent {\n<\/span><\/span><span class='shcb-loc'><span>  wrapper = viewChild.required&lt;ElementRef&lt;HTMLDivElement&gt;&gt;(<span class=\"hljs-string\">'wrapper'<\/span>);\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><mark class='shcb-loc'><span>  hasContent = computed(\n<\/span><\/mark><mark class='shcb-loc'><span>    <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> <span class=\"hljs-keyword\">this<\/span>.wrapper().nativeElement.innerHTML.length &gt; <span class=\"hljs-number\">0<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span>  );\n<\/span><\/mark><span class='shcb-loc'><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\">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 idea behind it is to wrap the <code><strong>ng-content<\/strong><\/code> within an element, and utilize the <code><strong>viewChild<\/strong><\/code> API to determine whether there is projected content or not.<\/p>\n\n\n\n<p>Then, based on that, the fallback content is displayed using the <code><strong>@if<\/strong><\/code> API provided by the <a href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/angular-control-flow-the-complete-guide\/\" class=\"ek-link\">new control flow<\/a>, or the <code><strong>ngIf<\/strong><\/code> directive.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note:<\/strong> in older versions of <strong>Angular<\/strong> you may need to refactor this a bit, but I hope the concept of this strategy is clear. If not please feel free to contact me <em>\ud83e\udef6\ud83c\udffb<\/em><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-angular-v18-fallback-content-in-ng-content\">Angular v18: fallback content in ng-content<\/h2>\n\n\n\n<p>Starting from <strong>Angular v18<\/strong>, implementing a <strong>fallback content<\/strong> in <code><strong>ng-content<\/strong><\/code> elements is extremely easier.<\/p>\n\n\n\n<p>You just need to put the <strong>fallback content<\/strong> inside the <code><strong>ng-content<\/strong><\/code> tag:<\/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 } <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-meta\">@Component<\/span>({\n<\/span><\/span><span class='shcb-loc'><span>  standalone: <span class=\"hljs-literal\">true<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>  selector: <span class=\"hljs-string\">'my-component'<\/span>,\n<\/span><\/span><span class='shcb-loc'><span>  template: <span class=\"hljs-string\">`<\/span>\n<\/span><\/span><mark class='shcb-loc'><span><span class=\"hljs-string\">    &lt;ng-content select=\"header\"&gt; Default header &lt;\/ng-content&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span><span class=\"hljs-string\"><\/span>\n<\/span><\/span><mark class='shcb-loc'><span><span class=\"hljs-string\">    &lt;ng-content&gt; Default main content &lt;\/ng-content&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span><span class=\"hljs-string\"><\/span>\n<\/span><\/span><mark class='shcb-loc'><span><span class=\"hljs-string\">    &lt;ng-content select=\"footer\"&gt; Default footer &lt;\/ng-content&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span><span class=\"hljs-string\">  `<\/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> MyComponent {}\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>As you can see, this new feature allows you to easily define a dedicated <strong>fallback content<\/strong> for each <code><strong>ng-content<\/strong><\/code> element in your component.<\/p>\n\n\n\n<p>So, considering the following example:<\/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 shcb-code-table shcb-line-numbers\"><span class='shcb-loc'><span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">my-component<\/span>&gt;<\/span>\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span>&gt;<\/span> New footer <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">my-component<\/span>&gt;<\/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\">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>The resulting template will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" 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>Default header\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>Default main content\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><mark class='shcb-loc'><span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span>&gt;<\/span> New footer <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<\/span><\/mark><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note:<\/strong> before <strong>Angular v18<\/strong>, this would result in an <strong>error<\/strong> being thrown <em>\u274c<\/em><\/p>\n<\/blockquote>\n\n\n<div class=\"wp-block-image caption-align-center\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1440\/1*_Vb4QbaIgC3J0ynVeMhAfg.jpeg\" alt=\"\" style=\"aspect-ratio:1;width:514px;height:auto\"\/><figcaption class=\"wp-element-caption\"><strong>TLDR<\/strong>: is this a good summary?&nbsp;\ud83e\udd2d<\/figcaption><\/figure><\/div>\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-fallback-content-strategies-limitations\">Fallback content strategies limitations<\/h3>\n\n\n\n<p>It is important to highlight that both strategies share the same limitation: <strong>Angular content projection is evaluated on component creation<\/strong>.<\/p>\n\n\n\n<p>This means that if you render the projected content conditionally, for example using an <code><strong>@if<\/strong><\/code> condition or the <code><strong>ngIf<\/strong><\/code> directive, the fallback content will not be displayed, even if the associated condition is set to <code><strong>false<\/strong><\/code>.<\/p>\n\n\n\n<p>This limitation can be easily overcome, with different solutions&nbsp;depending on the specific use case you might have, but it\u2019s important to remember about it when implementing the strategies I\u2019ve presented.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-thanks-for-reading-so-far-nbsp\">Thanks for reading so far&nbsp;\ud83d\ude4f<\/h2>\n\n\n\n<p>I\u2019d like to have your feedback so please feel free to contact me for any. <em>\ud83d\udc4f<\/em><\/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>Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. In this article, I will discuss about it. However, before diving into it, I will also explore how it was already possible to achieve the same result in Angular v17 or earlier versions. When building flexible and reusable UI&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\">Read more<\/a><\/p>\n","protected":false},"author":200,"featured_media":27670,"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,31],"tags":[],"collections":[],"class_list":{"0":"post-27610","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-languages","8":"category-web-developer","9":"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 fallback content in ng-content - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. Discover more here!\" \/>\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\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular fallback content in ng-content\" \/>\n<meta property=\"og:description\" content=\"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. Discover more here!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\" \/>\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-05-13T09:04:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-13T09:04:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"608\" \/>\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\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\"},\"author\":{\"name\":\"Davide Passafaro\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/79170e2a1bfc41ddeaa10827ce803828\"},\"headline\":\"Angular fallback content in ng-content\",\"datePublished\":\"2024-05-13T09:04:09+00:00\",\"dateModified\":\"2024-05-13T09:04:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\"},\"wordCount\":441,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png\",\"articleSection\":[\"Languages and frameworks\",\"Web Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\",\"name\":\"Angular fallback content in ng-content - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png\",\"datePublished\":\"2024-05-13T09:04:09+00:00\",\"dateModified\":\"2024-05-13T09:04:10+00:00\",\"description\":\"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. Discover more here!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png\",\"width\":1080,\"height\":608,\"caption\":\"Angular v18 fallback\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Frontend\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Web Developer\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Angular fallback content in ng-content\"}]},{\"@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 fallback content in ng-content - Codemotion Magazine","description":"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. Discover more here!","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\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/","og_locale":"en_US","og_type":"article","og_title":"Angular fallback content in ng-content","og_description":"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. Discover more here!","og_url":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2024-05-13T09:04:09+00:00","article_modified_time":"2024-05-13T09:04:10+00:00","og_image":[{"width":1080,"height":608,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.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\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/"},"author":{"name":"Davide Passafaro","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/79170e2a1bfc41ddeaa10827ce803828"},"headline":"Angular fallback content in ng-content","datePublished":"2024-05-13T09:04:09+00:00","dateModified":"2024-05-13T09:04:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/"},"wordCount":441,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png","articleSection":["Languages and frameworks","Web Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/","url":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/","name":"Angular fallback content in ng-content - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png","datePublished":"2024-05-13T09:04:09+00:00","dateModified":"2024-05-13T09:04:10+00:00","description":"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. Discover more here!","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png","width":1080,"height":608,"caption":"Angular v18 fallback"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Frontend","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/"},{"@type":"ListItem","position":3,"name":"Web Developer","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/"},{"@type":"ListItem","position":4,"name":"Angular fallback content in ng-content"}]},{"@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\/05\/image-600x400.png","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-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\/05\/image.png",1080,608,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-150x150.png",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-300x169.png",300,169,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-768x432.png",768,432,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-1024x576.png",1024,576,true],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png",1080,608,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image.png",1080,608,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-100x100.png",100,100,true],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-180x128.png",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-896x504.png",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-400x225.png",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-600x400.png",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/05\/image-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":"Angular v18 recently introduced a new feature to easily define a fallback content in ng-content elements. In this article, I will discuss about it. However, before diving into it, I will also explore how it was already possible to achieve the same result in Angular v17 or earlier versions. When building flexible and reusable UI&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/27610","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=27610"}],"version-history":[{"count":3,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/27610\/revisions"}],"predecessor-version":[{"id":27674,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/27610\/revisions\/27674"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/27670"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=27610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=27610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=27610"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=27610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}