{"id":1888,"date":"2017-03-03T10:23:48","date_gmt":"2017-03-03T10:23:48","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1888"},"modified":"2018-10-10T17:15:57","modified_gmt":"2018-10-10T17:15:57","slug":"using-ng-container-as-a-dummy-tag-to-simplify-markup-when-directives-are-used","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1888","title":{"rendered":"Using &lt;ng-container&gt; as a dummy tag to simplify markup when directives are used"},"content":{"rendered":"<p>Sometimes you need an element to hang e.g. an *ngIf directive on, but you don\u2019t want the element to appear in the markup.<\/p>\n<p>In the following example, we end up with an extra unwanted &lt;div&gt; level just because we needed to hang the *ngIf somewhere (noting that it is not permissible to have the *ngIf on the same element as the *ngFor).<\/p>\n<blockquote>\n<pre><span style=\"font-family: 'Lucida Console';\">&lt;div *ngFor=\"let place of places.rows\"&gt;\r\n  &lt;div *ngIf=\"place.doc.type == 'place'\"&gt;\r\n    &lt;span&gt;{{place.doc.name}}, &lt;\/span&gt;\r\n    &lt;span&gt;{{place.doc.strapline}}&lt;\/span&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;<\/span><\/pre>\n<\/blockquote>\n<p>Instead of a &lt;div&gt; we can use &lt;ng-container&gt;. This does not appear as an element in the markup (but does appear as a comment only) :-<\/p>\n<blockquote>\n<pre><span style=\"font-family: 'Lucida Console';\">&lt;div *ngFor=\"let place of places.rows\"&gt;\r\n  &lt;ng-container *ngIf=\"place.doc.type == 'place'\"&gt;\r\n    &lt;span&gt;{{place.doc.name}}, &lt;\/span&gt;\r\n    &lt;span&gt;{{place.doc.strapline}}&lt;\/span&gt;\r\n  &lt;\/ng-container&gt;\r\n&lt;\/div&gt;<\/span><\/pre>\n<\/blockquote>\n<p>This is analagous to the &lt;th:block&gt; dummy element in Thymeleaf, which does exactly the same thing.<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/39547858\/angular-2-ng-container\">This StackOverflow post<\/a> details this issue. <a href=\"http:\/\/stackoverflow.com\/questions\/19721395\/angularjs-how-to-use-ng-if-without-html-element\">This alternative post<\/a> mentions the use of ng-if-start and ng-if-end, but notes that these are undocumented in Angular 2 so may not be fully supported.<\/p>\n<p>For a related issue where nested loops are in use causing a similar problem, <a href=\"http:\/\/stackoverflow.com\/questions\/25010244\/angularjs-creating-dummy-directive\">this post<\/a> details the use of <em>ng-repeat-start<\/em> and<em> ng-repeat-end<\/em> as an alternative solution.<\/p>\n<p><em><strong>Note however that ng-repeat-start\/ng-repeat-end only works for Angular 1.<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need an element to hang e.g. an *ngIf directive on, but you don\u2019t want the element to appear in the markup. In the following example, we end up with an extra unwanted &lt;div&gt; level just because we needed to hang the *ngIf somewhere (noting that it is not permissible to have the *ngIf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[198],"tags":[199,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1888"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1888"}],"version-history":[{"count":6,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions"}],"predecessor-version":[{"id":1907,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions\/1907"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}