0

This is my first question here and I'm only asking because I really couldn't find an answer anywhere - or couldn't figure out how to make it work for me at least (I'm a huge noob when it comes to HTML and Ionic).

Here's the thing: I'm using Ionic and I want to make a text variable work as an HTML object. Instead, it ends up being merely displayed as text (with tags and everything) on the page.

TypeScript (dice.ts)

export class Dice {

...

dice = 4;
result = 0;
teste = "<h2>Teste</h2>";

...

HTML (dice.html)

<ion-content padding>

<h1> TITULO </h1>
{{teste}}

...

</ion-content>

Outcome

TITULO
<h2>Teste</h2>

In other words, it did not create a "header2", but just showed the original string.

2 Answers 2

1

You can try something like this:

<ion-content padding>
  <h1> TITULO </h1>
  <div ng-bind-html-unsafe="teste"></div>
</ion-content>

also, some more reference: AngularJS : Insert HTML into view

Sign up to request clarification or add additional context in comments.

2 Comments

I did try this before and now, once again, tried it with your code (copy-pasted it) and the text simply disappeared. Instead of displaying the header correctly, it showed absolutely nothing. It simply printed "TITULO" (as a "header1") and nothing else. Thank you very much, anyway!
You are using angular2, check this one instead stackoverflow.com/a/32341540/3760661
0

Ogari, Please try

<div [innerHTML]={{teste}}></div>

You will find more about this at this link

1 Comment

Thanks, it worked perfectly! And sorry it took me so long for the feedback. I had already solved my issue with a workaround using ngContainer and ngFor, but now it's much simpler.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.