I am trying to sanitize text thats coming from db i dont control and show it as html. My broken code looks as follows.
<li *ngFor="let comm of commList|async;">
<div class="notifBody">
<span>
<div [innerHTML]={{comm.text|safe : 'html'}}> </div>
</span>
</div>
</li>
The error i am getting is as follows
compiler.es5.js:1694 Uncaught Error: Template parse errors:
Unexpected closing tag "div". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<hr>
<span>
<div [innerHTML]={{comm.text|safe : 'html'}}>[ERROR ->]</div>
</span>
</div>
I am guessing i am missing something in my syntax but not sure what.
If i remove the safe pipe as follows it works fine and renders the text as html....(not sanitized).
<div class="notifBody">
<span>
<div [innerHTML]=comm.text></div>
</span>
</div>
The following pipe test also works as expected but the text wont be rendered as html.
<div class="notifBody">
<span>
{{comm.text|safe : 'html'}}
</span>
</div>
The Safe pipe just sanitizes the passed string using Sanitizer from angular core as follows.
import { Pipe, Sanitizer, SecurityContext } from '@angular/core';
...
public transform(value: string, type: string): string {
switch (type) {
case 'html':
return this.sanitizer.sanitize(SecurityContext.HTML, value);
...}}
Thanks!
*ngFor="let comm of commList|async;>?<div innerHTML={{comm.text|safe : 'html'}}></div>, without square brackets