0

I need to conditionally add an extra html as a div content. For now I did it this way:

before:

<div *ngFor="let cell of cells" class="cell"
    [innerHTML]="cell.dataWithHtml"
></div>

after:

<div *ngFor="let cell of cells" class="cell">
    <img *ngIf="cell.isSpecial" src="whatever"/>
    <div class="this_div_should_not_exist" [innerHTML]="cell.dataWithHtml"></div>
</div>

But this adds an extra div.

How this block can be rewritten without adding extra div?

6
  • how about using interpolation? {{cell.dataWithHtml}} Commented Apr 6, 2020 at 10:18
  • it will escape html characters Commented Apr 6, 2020 at 10:19
  • Possible duplicate question stackoverflow.com/questions/39857858/… Commented Apr 6, 2020 at 10:22
  • It is not a question about escaping html. This is a question about adding extra conditional html to inner html, I think I can try to use a pipe, for adding extra html. Commented Apr 6, 2020 at 10:29
  • you can use <span [innerHTML]=...></span> Commented Apr 6, 2020 at 10:39

1 Answer 1

1

Use outerHTML on the div you wish to disappear/replace - you can't use ng-container.

<div class="this_div_should_not_exist" [outerHTML]="cell.dataWithHtml"></div>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.