7

Currently I'm using

htmlToText(html: string) {
    const tmp = document.createElement('DIV');
    tmp.innerHTML = html;
    return tmp.textContent || tmp.innerText || '';
}

for the task. What's the Angular way of doing that? Or is it perfectly fine to do like that? Can directly accessing document like that lead to problems, e.g. with mobile apps?

Note: There's an AngularJS related question, but I'm looking for an Angular2+ anwer. Plus I'm not sure whether the regex from that accepted answer is the way to go?

1
  • 1
    after 6 years more experience looking back at this question I'd say: It's fine like this. why would I create a pipe if it's for a one time usage? from an interop perspective I don't see problems. and tmp should be garbage collected, so no problem there either. Commented Apr 20, 2023 at 20:42

5 Answers 5

4

you should create a pipe like this:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'htmlToPlaintext'
})
export class HtmlToPlaintextPipe implements PipeTransform {
    transform(value: any): string {
        const temp = document.createElement('div');
        temp.innerHTML = value;
        return temp.textContent || temp.innerText || '';
    }
}

and use this pipe like this:

{{htmlCode | htmlToPlaintext}}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Can you please explain that how your answer is different from other answers of this page?
4

Create a pipe

    import { Pipe, PipeTransform } from '@angular/core';
    @Pipe({name: 'htmlToPlaintext'})
    export class HtmlToPlaintextPipe implements PipeTransform {
      transform(value: string): string {
        return value? value.replace(/]+>/gm, '') : '';
      }
    }

and use this pipe in your template

    {{yourAttribute | htmlToPlaintext}}

In Angular, you should not modify the dom directly from your component. It should always be possible to use the template to achieve what you want.

Comments

1

@bersling I had a similar situation but I use a template variable to achieve the same.

      <p #desc
        class="[ hit hit__description ]"
        [innerHTML]="hit.shortText"
        [matTooltip]="desc.innerText"
        [matTooltipDisabled]="desc.innerText.length < 140"
      ></p>

1 Comment

Exactly I was looking for.. thanks :)
0

What you need to do is escape these characters, then you archive what you want:

" replace with &quot;
& replace with &amp;
< replace with &lt;
> replace with &gt;

more about escape characters

XML / HTML / XHTML or any SGML derivat will not parse these characters as what they intended for.

Comments

0

To render html as a string in Angular 2+, we can use the innerHTML property. For example:

<div [innerHTML]="htmlString"></div>

Or, have a look at this converter: https://www.npmjs.com/package//html-to-text

More information: Angular HTML binding

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.