{"id":1212,"date":"2023-07-20T05:00:00","date_gmt":"2023-07-20T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1212"},"modified":"2023-07-19T12:27:55","modified_gmt":"2023-07-19T19:27:55","slug":"what-is-ng-container","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/what-is-ng-container\/","title":{"rendered":"What is ng-container?"},"content":{"rendered":"\n<p>Yesterday, we covered <code><a href=\"https:\/\/blog.angulartraining.com\/what-is-ng-template-and-when-to-use-it-f875b46aa078?sk=44771004469a9dc9e28d8976f22ca3ed\" target=\"_blank\" rel=\"noopener\" title=\"\">ng-template<\/a><\/code> and touched on different scenarios when it makes sense to use <code>ng-template<\/code>. Let&#8217;s now introduce a close relative of <code>ng-template<\/code> called <code><a href=\"https:\/\/angular.io\/api\/core\/ng-container\" target=\"_blank\" rel=\"noopener\" title=\"\">ng-container<\/a><\/code>.<\/p>\n\n\n\n<p>There are a lot of similarities between those two elements, which can both be used to use structural directives such as ngIf or ngFor, with the bonus that ng-container can be used with the shorthand syntax of those directives:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"473\" height=\"35\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-19.png\" alt=\"\" class=\"wp-image-1213\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-19.png 473w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-19-300x22.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>One thing you probably noticed as an Angular developer is that we can&#8217;t use two structural directives on the same element:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"35\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-20.png\" alt=\"\" class=\"wp-image-1214\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-20.png 593w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-20-300x18.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>In this scenario, using <code>ng-container<\/code> is the perfect way to use both directives without adding any HTML to the DOM, as <code>ng-container<\/code>, just like <code>ng-template<\/code>, doesn&#8217;t create new DOM elements:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"168\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-21.png\" alt=\"\" class=\"wp-image-1215\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-21.png 468w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-21-300x108.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Another use for <code>ng-container<\/code> is to become the host of a <code>ng-template<\/code>, similar to the way that a <code>router-outlet<\/code> is the host of routed components:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"80\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-22.png\" alt=\"\" class=\"wp-image-1216\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-22.png 538w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-22-300x45.png 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Such templates can be passed from a parent component using <code>@Input<\/code> and let the child component decide when to display which template, which is very powerful:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"438\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-23.png\" alt=\"\" class=\"wp-image-1217\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-23.png 740w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-23-300x178.png 300w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can find a <a href=\"https:\/\/stackblitz.com\/edit\/ng-template-outlets?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fstate-button%2Fstate-button.component.html,src%2Fapp%2Fstate-button%2Fstate-button.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">complete code example of such a feature in this Stackblitz<\/a> and a more in-depth tutorial here if you want to try it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we covered ng-template and touched on different scenarios when it makes sense to use ng-template. Let&#8217;s now introduce a close relative of ng-template called ng-container. There are a lot of similarities between those two elements, which can both be used to use structural directives such as ngIf or ngFor, with the bonus that ng-container [&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-1212","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\/1212","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=1212"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1212\/revisions"}],"predecessor-version":[{"id":1219,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1212\/revisions\/1219"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}