{"id":717,"date":"2017-09-05T14:19:00","date_gmt":"2017-09-05T14:19:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=717"},"modified":"2025-02-21T07:58:21","modified_gmt":"2025-02-21T07:58:21","slug":"content-projection-in-angular-2","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/content-projection-in-angular-2","title":{"rendered":"Simplifying Content Projection in Angular"},"content":{"rendered":"\n<p>Content projection allows you to insert a shadow DOM in your component. To put it simply, if you want to insert HTML elements or other components in a component, then you do that using concept of content projection. In Angular, you achieve content projection using <b>&lt; ng-content &gt;&lt; \/ng-content &gt;.&nbsp; <\/b>You can make reusable components and scalable application by right use of content projection.<b><\/b><\/p>\n\n\n\n<p>To understand content projection, let us consider <b>GreetComponent<\/b> as shown in the code listing below:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import\u00a0{\u00a0Component\u00a0}\u00a0from\u00a0'@angular\/core';\n \n@Component({\n\u00a0\u00a0\u00a0\u00a0selector:\u00a0'greet',\n\u00a0\u00a0\u00a0\u00a0template:\u00a0`{{message}}`\n})\nexport\u00a0class\u00a0GreetComponent\u00a0{\n \n\u00a0\u00a0\u00a0\u00a0message:\u00a0string\u00a0=\u00a0\"Hello\u00a0There\u00a0!\"\n \n}<\/pre>\n\n\n\n<p>Now if you use GreetComponent in another component, and want to pass a greeting message from the parent component, then you should use the <b>@Input()<\/b> decorator. This way, a modified GreetComponnet will look like the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import\u00a0{\u00a0Component,\u00a0Input\u00a0}\u00a0from\u00a0'@angular\/core';\n \n@Component({\n\u00a0\u00a0\u00a0\u00a0selector:\u00a0'greet',\n\u00a0\u00a0\u00a0\u00a0template:\u00a0`{{message}}`\n})\nexport\u00a0class\u00a0GreetComponent\u00a0{\n \n\u00a0\u00a0\u00a0\u00a0@Input()\u00a0message:\u00a0string;\n \n}<\/pre>\n\n\n\n<p>Using the @Input() decorator, you can pass a simple string to the GreetComponnet, but what if you need to pass different types of data to the GreetComponent such as:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Inner HTML<\/li>\n\n\n\n<li>HTML Elements<\/li>\n\n\n\n<li>Styled HTML<\/li>\n\n\n\n<li>Another Component etc.<\/li>\n<\/ol>\n\n\n\n<p>To pass or project styled HTML or another component, content projection is used. Let us modify the GreetComponent to use content projection in this code:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"374\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image.png\" alt=\"Let us modify the GreetComponent to use content projection in this code\" class=\"wp-image-1696\" title=\"Let us modify the GreetComponent to use content projection in this code\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image.png 650w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-300x173.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-480x276.png 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p>We are using to project content in the GreetComponent. When you use the GreetComponent, you\u2019ll pass content as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"436\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-1.png\" alt=\"When you use the GreetComponent, you\u2019ll pass content as shown\" class=\"wp-image-1697\" title=\"When you use the GreetComponent, you\u2019ll pass content as shown\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-1.png 650w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-1-300x201.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-1-480x322.png 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p>In the listing above, you are projecting styled HTML to the GreetComponent and you\u2019ll get the following output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"287\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-2.png\" alt=\"the listing above, you are projecting styled HTML to the GreetComponent and you\u2019ll get the following output\" class=\"wp-image-1699\" title=\"the listing above, you are projecting styled HTML to the GreetComponent and you\u2019ll get the following output\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-2.png 593w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-2-300x145.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-2-480x232.png 480w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<p>This is an example of <b>Single Slot Content Projection<\/b>. Whatever you pass to the GreetComponent will be projected. So, let us pass more than one HTML element to the GreetComponent as shown in the listing below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"427\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-3.png\" alt=\" let us pass more than one HTML element to the GreetComponent as shown\" class=\"wp-image-1700\" title=\" let us pass more than one HTML element to the GreetComponent as shown\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-3.png 650w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-3-300x197.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-3-480x315.png 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p>Here we are passing three HTML elements to the GreetComponent, and all of them will be projected. You will get the output as shown in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"281\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-4.png\" alt=\"we are passing three HTML elements to the GreetComponent, and all of them will be projected.\" class=\"wp-image-1701\" title=\"we are passing three HTML elements to the GreetComponent, and all of them will be projected.\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-4.png 573w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-4-300x147.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-4-480x235.png 480w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/figure>\n\n\n\n<p>In the DOM, you can see that inside the GreetComponent, all HTML elements are projected.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"301\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-5.png\" alt=\"you can see that inside the GreetComponent, all HTML elements are projected\" class=\"wp-image-1702\" title=\"you can see that inside the GreetComponent, all HTML elements are projected\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-5.png 594w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-5-300x152.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-5-480x243.png 480w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"multi-slot-projection\">Multi Slot Projection<\/h2>\n\n\n\n<p>You may have a requirement to project elements in multiple slots of the component. In this next example, let\u2019s say you want to create a greeting card like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"275\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-6.png\" alt=\"let\u2019s say you want to create a greeting card like this\" class=\"wp-image-1703\" title=\"let\u2019s say you want to create a greeting card like this\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-6.png 608w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-6-300x136.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-6-480x217.png 480w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/figure>\n\n\n\n<p>This can be created using the component as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"349\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-7.png\" alt=\"This can be created using the component as shown\" class=\"wp-image-1704\" title=\"This can be created using the component as shown\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-7.png 650w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-7-300x161.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-7-480x258.png 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p>Let us say we have a requirement to pass the header element and a btn element so the name and button can be dynamically passed to the GreetComponent. &nbsp;This time, we need two slots:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A slot for the header element<\/li>\n\n\n\n<li>A slot for the btn element<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s modify the GreetComponent to cater to the above requirement as shown in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"554\" height=\"325\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-8.png\" alt=\"Let\u2019s modify the GreetComponent to cater to the above requirement as shown in the image\" class=\"wp-image-1705\" title=\"Let\u2019s modify the GreetComponent to cater to the above requirement as shown in the image\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-8.png 554w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-8-300x176.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-8-480x282.png 480w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><\/figure>\n\n\n\n<p>Here we\u2019re using &nbsp;ng-content two times. Now, the question is do we select a particular ng-content to project the &nbsp;h1 element and another ng-content to project a btn &nbsp;element?<\/p>\n\n\n\n<p>You can select a particular slot for projection using the &nbsp;<b>ng-content<\/b>&gt; selector.&nbsp; There are four types of selectors:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Project using tag selector<\/li>\n\n\n\n<li>Project using class selector<\/li>\n\n\n\n<li>Project using id selector<\/li>\n\n\n\n<li>Project using attribute selector&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>You can use the tag selector for multi slot projection as shown in the listing below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"843\" height=\"450\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-9.png\" alt=\"You can use the tag selector for multi slot projection as shown in the listing\" class=\"wp-image-1706\" title=\"You can use the tag selector for multi slot projection as shown in the listing\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-9.png 843w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-9-300x160.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-9-768x410.png 768w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-9-480x256.png 480w\" sizes=\"auto, (max-width: 843px) 100vw, 843px\" \/><\/figure>\n\n\n\n<p>Next, you can project content to the GreetComponent as shown in the listing below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"769\" height=\"291\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-10.png\" alt=\"you can project content to the GreetComponent as shown in the listing\" class=\"wp-image-1707\" title=\"you can project content to the GreetComponent as shown in the listing\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-10.png 769w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-10-300x114.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-10-480x182.png 480w\" sizes=\"auto, (max-width: 769px) 100vw, 769px\" \/><\/figure>\n\n\n\n<p>As you can see, we are using the GreetComponent twice and projecting different h1 and button elements. You\u2019ll get the output as shown in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"313\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-11.png\" alt=\"You\u2019ll get the output as shown in the image\" class=\"wp-image-1708\" title=\"You\u2019ll get the output as shown in the image\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-11.png 650w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-11-300x144.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-11-480x231.png 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p>The problem with using tag selectors is that all h1 elements will get projected to the GreetComponent. In many scenarios, you may not want that and can use other selectors such as a class selector or an attribute selector, as shown in the listing below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"471\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-12.png\" alt=\"you may not want that and can use other selectors such as a class selector or an attribute selector, as shown in the listing below\" class=\"wp-image-1709\" title=\"you may not want that and can use other selectors such as a class selector or an attribute selector, as shown in the listing below\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-12.png 820w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-12-300x172.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-12-768x441.png 768w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-12-480x276.png 480w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/figure>\n\n\n\n<p>Next, you can project content to the GreetComponent as shown in the listing below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"326\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-13.png\" alt=\"you can project content to the GreetComponent as shown in the listing\" class=\"wp-image-1710\" title=\"you can project content to the GreetComponent as shown in the listing\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-13.png 806w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-13-300x121.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-13-768x311.png 768w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-13-480x194.png 480w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/figure>\n\n\n\n<p>You\u2019ll get the same output as above, however this time you are using the class name and attribute to project the content. When you inspect an element on the DOM, you will find the attribute name and the class name of the projected element as shown in the image below:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"218\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-14.png\" alt=\"When you inspect an element on the DOM, you will find the attribute name and the class name of the projected element as shown\" class=\"wp-image-1711\" title=\"When you inspect an element on the DOM, you will find the attribute name and the class name of the projected element as shown\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-14.png 852w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-14-300x77.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-14-768x197.png 768w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2017\/09\/image-14-480x123.png 480w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/figure>\n\n\n\n<p>Content projection is very useful to insert shadow DOM in your components. To insert HTML elements or other components in a component, you need to use content projection. In AngularJS 1.X, content projection was achieved using <b>Transclusion<\/b>, however in Angular it is achieved using &lt;<b>ng-content&gt;<\/b><\/p>\n\n\n\n<p>In the next post, you will learn about more important concepts&nbsp;in Angular, so stay tuned. Also, do not forget to check out&nbsp;<a href=\"\/products\/ignite-ui\">Ignite UI for JavaScript\/HTML5 and ASP.NET MVC<\/a>, which you can use&nbsp;with HTML5, Angular, React, and ASP.NET MVC to create rich Internet applications. You can&nbsp;<a href=\"\/products\/ignite-ui\/download\">download a trial<\/a>&nbsp;of all our JavaScript controls for free!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Angular, content projection\u00a0is used to project content in a component. Let\u2019s take a closer look at how it works:<\/p>\n","protected":false},"author":65,"featured_media":1166,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=717"}],"version-history":[{"count":4,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/717\/revisions"}],"predecessor-version":[{"id":2132,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/717\/revisions\/2132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/1166"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}