64

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

3 Answers 3

204

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.

2
  • 1
    gosh, why didn't I think of this. Been racking brain for hours trying to figure something out. Changed my div to a ng-container and now I can use simple css to achieve what I was trying to do
    – Ronnie
    Feb 27, 2020 at 0:31
  • For those who came from react, think of ng-container as angular equivalent to React.Fragment in React Dec 31, 2021 at 9:34
6

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

6
  • The second option is probably more what im looking for... im looking to just dump the json in between the ul tags.. will doing it that way output raw html and do I use the second method by making a directive... sorry pretty new to angular so I may be using the wrong term
    – Kravitz
    May 3, 2016 at 13:16
  • No worries. Using the innerHTML attribute is possible at the level of the ngFor ;-) I updated my answer... May 3, 2016 at 13:19
  • I added a plunkr demonstrating this May 3, 2016 at 13:20
  • Thanks dude, so I made a new directive and this is working for me @Component({ selector: 'social-wall', template: <template ngFor [ngForOf]="socialwall | async" #social > {{ social.tile_content }} </template>, }) but its not plain html any idea how i work that in at this level
    – Kravitz
    May 3, 2016 at 13:56
  • I think that the innerHTML attribute is what you should use. It will allow you to render the HTML... May 3, 2016 at 14:04
5

You can use outerHTML

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

Plunker example

6
  • Not quite what im after.. you see the li tag will be supplied by the json result - I essentially need to dump the json between the ul tags as raw html
    – Kravitz
    May 3, 2016 at 13:17
  • Sorry, forgot to move the *ngFor to the <li> element. Now it produces the expected HTML. May 3, 2016 at 13:19
  • Did you check the Plunker? It's now generating <li> that are not wrapped with <ul> May 3, 2016 at 13:45
  • Cheers man, I ended up using this as a base point and scrapped the idea of creating a new directive.
    – Kravitz
    May 3, 2016 at 14:48
  • 1
    The Plunker example worked back then when I posted the answer. Jul 28, 2021 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.