Angular 2 - ngfor without wrapping in a container

Angular

Angular Problem Overview


I wish the have a ul element and then in my json I have html containing list (li) elements from various social media sites.. when I put ngfor on the list items is creates multiple ul's for each of the outputted json records.. is it possible just to output each of the li's as plain html and not wrap them in an element

small sample of my json

{
	"courses": [{
		"tile_id": 0,
		"tile_content": "<li class=\"grid-item horiz-double blog-tile\" data-item-id=\"0\">yrdy<\/li>"
	}],
}

small sample of my angular service - this works perfectly and outputs the json but maybe im missing some kind of formatting setting

public getCourses() {

    this._http.get(this.apiUrl).map(response => response.json()).subscribe(data => {
        // console.log( data.courses );
        this._dataStore.courses = data.courses;

        // console.log(this._coursesObserverXX);

        this._coursesObserverXX.next(this._dataStore.courses);

    }, error => console.log('Could not load courses.'));

}

and my html

<ul class="list-group" *ngFor="#course of courses | async">
      {{ course.tile_content }}
</ul>

the goal is to not have the ngfor output a wrapper and also to display the output as plain html rather than plaintext as it appears to come out

Angular Solutions


Solution 1 - Angular

In some cases, <ng-container> may be useful. Like (not for this specific question):

<ng-container *ngFor="let item of items; let i = index">
  ...
</ng-container>

In the DOM, its content is rendered as usual, but the tag itself is rendered as an HTML comment.

From the documentation:

> ### Group sibling elements with <ng-container> > > [...] > > <ng-container> to the rescue > > The Angular <ng-container> is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM.

Solution 2 - Angular

Not sure to understand what you want / try to do but you can do something like this:

<ul class="list-group">
  <li *ngFor="#course of courses | async">
    {{ course.tile_content }}
  </li>
</ul>

Note that the expanded syntax could also be used:

<template ngFor [ngForOf]="courses | async" #course>
  {{ course.tile_content }}
</template>

(the template tag won't be added into the HTML)

Edit

This works:

<ul *ngFor="#elt of elements | async" [innerHTML]="elt.title">
</ul>

See this plunkr: https://plnkr.co/edit/VJWV9Kfh15O4KO8NLNP3?p=preview

Solution 3 - Angular

You can use outerHTML

<ul class="list-group" >
  <li *ngFor="let course of courses | async" [outerHtml]="course.tile_content"></li>
</ul>

Plunker example

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKravitzView Question on Stackoverflow
Solution 1 - AngularArjanView Answer on Stackoverflow
Solution 2 - AngularThierry TemplierView Answer on Stackoverflow
Solution 3 - AngularGünter ZöchbauerView Answer on Stackoverflow