0

I want to know how can I use raw HTML tags inside the ngOnInit(). For example, normally I could use something like that:

element.whateverIwant + '<br>';

But when I use it inside of ngOnInit() the <br> becomes just normal text.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    That's Angular's security preventing risky content injection. If you're sure the content is OK, tell Angular you know that using the [`DomSanitizer`](https://angular.io/api/platform-browser/DomSanitizer). – jonrsharpe Sep 01 '17 at 20:30
  • 1
    Possible duplicate of [angular 2 html binding](https://stackoverflow.com/questions/31548311/angular-2-html-binding) – jonrsharpe Sep 01 '17 at 20:32
  • Ohhh... but what if I want to keep the security in place, how should I do this right then? Perhaps using some other way for output? – wofuziwibu Sep 01 '17 at 20:32
  • How should you do *what* right? It's unclear precisely what you're trying to achieve - please [edit] to give context. And read the answers on the suggested dupe to see if that clarifies what you need to know. – jonrsharpe Sep 01 '17 at 20:33
  • pal, I'm trying to output some data with html tags... I showed this with an example... Gonna look at the other question – wofuziwibu Sep 01 '17 at 20:35
  • `element.whateverIwant` doesn't illustrate much, though, does it? Again, what's the **context**? Where does `element` come from? Either you can trust it, in which case use the DomSanitizer, or you can't, in which case you *why do you want it in the DOM?* – jonrsharpe Sep 01 '17 at 20:37
  • take the elementRef on your component constructor and set innerHtml on the elementRef.nativeElement. – Sumit Agarwal Sep 02 '17 at 10:02

0 Answers0