-3

I am receiving a string from the API

textInsert = "<p>Hello</p><br><p>There</p>"

I wish to use the with

  <ion-text color="secondary">
    {{textInsert}}
  </ion-text>

How would I convert the string to

<p>Hello</p><br><p>There</p>

to use in the ion-text

3
  • And what happens when you do that? Commented Aug 27 at 18:21
  • @TimRoberts I don't use Ionic, but I'm guessing they're seeing the HTML tags literally, rather than having them rendered. This is presumably a XSS protection. Commented Aug 27 at 18:33
  • ion-text uses html inside the > <. If just use a string it just displays the string. If use <ion-text color="secondary"> <p>Hello</p><br><p>There</p></ion-text> directly it displays properly. This was not my first choice but this is the only thing the API can send textInsert = "<p>Hello</p><br><p>There</p>" Commented Aug 27 at 18:58

2 Answers 2

1

In Ionic, the <ion-text> component is mostly a wrapper that applies Ionic’s theming to text—it doesn’t parse or render raw HTML strings automatically. So if you try something like this:

<ion-text>
  "{{ '<b>Bold</b>' }}"
</ion-text>

…it will just display "<b>Bold</b>" as text, not bold text.


Correct way: Use Angular’s property binding with [innerHTML] if you want to render HTML inside ion-text:

<ion-text [innerHTML]="htmlContent"></ion-text>

And in your component:

htmlContent = '<b>This is bold text</b> and <i>this is italic</i>';
Sign up to request clarification or add additional context in comments.

Comments

0

Here is what I found that works. ionic no longer supports sanitizers, use Angular and [innerHTML]. Then insert into label, div etc..

.ts

import { DomSanitizer } from '@angular/platform-browser';
import { Component, inject } from '@angular/core';

export class HelpPage {
  sanitizer = inject(DomSanitizer);

textInsert = "<p>Hello</p><br><p>There</p>"

.html

<ion-text [innerHTML]="sanitizer.bypassSecurityTrustHtml(textInsert)" ></ion-text>

Comments

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.