1

I have a string stored in my localStorage with the HTML tags since I use ngx-quill (Angular Rich Text Editor) which stores the data in HTML formatted text.

This is what my localStorage looks like: enter image description here

As you can see from the picture above, in description, there is p tags.

Is there any way I can display it on Angular page without the tags? enter image description here

1
  • 3
    You should user [innerHTML] for that, for example: <div [innerHTML]="item.description"></div> Commented Nov 18, 2021 at 6:38

2 Answers 2

3

Use: [innerHTML] for that, for example:

<div [innerHTML]="data.description"></div>
Sign up to request clarification or add additional context in comments.

Comments

3

You need to use innerHTML and make sure that you trust the HTML if it contains script tag else Angular will throw an error.

Do the following, create a safeHTML pipe:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

    @Pipe({ name: 'sanitizeHtml' })
    export class SanitizeHtmlPipe implements PipeTransform {
    
        constructor(private sanitizer: DomSanitizer) { }
    
        transform(html: string): any {
            return this.sanitizer.bypassSecurityTrustHtml(html);
        }
    }

and then use this with innerHTML like this:

<span [innerHTML]="description" | sanitizeHtml"></span>

1 Comment

that is not how you sanitize, that's how you 'trust' content as-is which you shouldn't. please refer to angular.io/api/platform-browser/DomSanitizer#sanitize

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.