{"id":1946,"date":"2022-06-07T13:02:48","date_gmt":"2022-06-07T11:02:48","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1946"},"modified":"2022-06-08T17:51:44","modified_gmt":"2022-06-08T15:51:44","slug":"how-to-use-ngtemplateoutlet-in-angular","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/06\/07\/how-to-use-ngtemplateoutlet-in-angular\/","title":{"rendered":"How to Use ngTemplateOutlet in Angular"},"content":{"rendered":"\n<p><code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">ngTemplateOutlet<\/span><\/em><\/code>&nbsp;&nbsp;is a structural directive. It instantiates a template dynamically using a template reference and context object as parameters. We use it to insert a template (created by&nbsp;<code>ngTemplate<\/code>) in various sections of our DOM.<\/p>\n\n\n\n<h4>Defining a Template<\/h4>\n\n\n\n<p>Before we can use&nbsp;<code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">ngTemplateOutlet<\/span><\/em><\/code>&nbsp;we must first define a template using&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>&lt;ng-template&gt;<\/code>.<\/span> The template is the body of the&nbsp;<code>&lt;ng-template&gt;<\/code>&nbsp;element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ng-template #myTemplate&gt;\n  &lt;div&gt;Hello template&lt;\/div&gt;\n&lt;\/ng-template&gt;\n<\/code><\/pre>\n\n\n\n<p>To reference the template we name it via&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>#<\/code>&nbsp;<\/span>syntax. By adding&nbsp;<code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">#myTemplate<\/span><\/em><\/code>&nbsp;to the element we can get a reference to the template using the name<em><span class=\"has-inline-color has-luminous-vivid-orange-color\">&nbsp;<code>myTemplate<\/code><\/span><\/em>&nbsp;. The type of&nbsp;<code>myTemplate<\/code>&nbsp;is&nbsp;<code><a rel=\"noreferrer noopener\" href=\"https:\/\/angular.io\/api\/core\/TemplateRef\" target=\"_blank\">TemplateRef<\/a><\/code>.<\/p>\n\n\n\n<h4>Rendering a Template<\/h4>\n\n\n\n<p>The content of a&nbsp;<code>&lt;ng-Template&gt;<\/code>&nbsp;element is not rendered in the browser. To have the template body rendered we must now pass the template reference to a&nbsp;<code>ngTemplateOutlet<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- Define our template --&gt;\n&lt;ng-template #myTemplate&gt; World! &lt;\/ng-template&gt;\n\nHello\n&lt;!-- Render the template in this outlet --&gt;\n&lt;ng-container &#91;ngTemplateOutlet]=\"myTemplate\"&gt;&lt;\/ng-container&gt;\n<\/code><\/pre>\n\n\n\n<h4>Passing data to ngTemplateOutlet<\/h4>\n\n\n\n<p>We can also pass data to the template by using <em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>ngTemplateOutletContext<\/code>.<\/span><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ng-container &#91;ngTemplateOutlet]=\"myTemplate\" \n       &#91;ngTemplateOutletContext] =\"{value:'1000'}\"&gt;\n&lt;\/ng-container&gt;  <\/code><\/pre>\n\n\n\n<p>Alternatively we can also use the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n \n&lt;ng-container *ngTemplateOutlet=\"myTemplate; context:{value:100}\"&gt;\n&lt;\/ng-container&gt; <\/code><\/pre>\n\n\n\n<h4>Using the Context in the template<\/h4>\n\n\n\n<p>To access the context in our template we use&nbsp;<code>let-*<\/code>&nbsp;syntax to define&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/angular.io\/guide\/structural-directives#template-input-variable\" target=\"_blank\">template input variables<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ng-template #myTemplate let-params=\"value\"&gt;\n  &lt;div&gt;Hello template {{ params }} &lt;\/div&gt;\n&lt;\/ng-template&gt;<\/code><\/pre>\n\n\n\n<h4>Using $implicit<\/h4>\n\n\n\n<p>If you use the key&nbsp;<code>$implicit<\/code>&nbsp;in the context object will set its value as default for all the local variables.<\/p>\n\n\n\n<p>For Example we have not assigned anything to the&nbsp;<code>let-name<\/code>&nbsp;so it will take the value from the&nbsp;<code>$implicit<\/code>, which is&nbsp;<code>Guest<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng-template let-name let-message=\"message\" #myTemplate&gt;  \n  &lt;p&gt;Dear {{name}} , {{message}} &lt;\/p&gt;\n&lt;\/ng-template&gt;\n \n&lt;ng-container &#91;ngTemplateOutlet]=\"myTemplate\" \n              &#91;ngTemplateOutletContext] =\"{$implicit:'Guest',message:'Welcome to our site'}\"&gt;\n&lt;\/ng-container&gt; <\/code><\/pre>\n\n\n\n<p>And in the following code, both&nbsp;<code>name<\/code>&nbsp;&amp;&nbsp;<code>message<\/code>&nbsp;gets the value from the&nbsp;<code>$implicit<\/code>&nbsp;i.e&nbsp;<code>Guest<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ng-template let-name let-message #musterTemplate&gt;  \n  &lt;p&gt;Dear {{name}} , {{message}} &lt;\/p&gt;\n&lt;\/ng-template&gt;\n \n \n&lt;ng-container &#91;ngTemplateOutlet]=\"musterTemplate\" \n      &#91;ngTemplateOutletContext] =\"{$implicit:'Guest',message:'Welcome to our site'}\"&gt;\n&lt;\/ng-container&gt; <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ngTemplateOutlet&nbsp;&nbsp;is a structural directive. It instantiates a template dynamically using a template reference and context object as parameters. We use it to insert a template (created by&nbsp;ngTemplate) in various sections of our DOM. Defining a Template Before we can use&nbsp;ngTemplateOutlet&nbsp;we must first define a template using&nbsp;&lt;ng-template&gt;. The template is the body of the&nbsp;&lt;ng-template&gt;&nbsp;element. To reference [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37],"tags":[558,469,556,557],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1946"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1946"}],"version-history":[{"count":2,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1946\/revisions"}],"predecessor-version":[{"id":1966,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1946\/revisions\/1966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}