1

I am aware that HTML can be retrieved using [(ngModel)]="htmlContent", but is it possible to get just the text? Thanks.

Example:

This text is bold. This is italics

html content: <b>This text is bold</b>.<i>This is italics</i>

text content: This text is bold. This is italics

Raised github issue

dasfdsa
  • 7,102
  • 8
  • 51
  • 93

4 Answers4

4

You can parse it with the DOMParser class, and then just use the innerText property.

Assuming you have the HTML in a variable called html, it would be

let html = '<b>This text is bold</b>.<i>This is italics</i>';

var oParser = new DOMParser();
var oDOM = oParser.parseFromString(html, "text/html");
var text = oDOM.body.innerText;

console.log(text);

More about the parser can be found at https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML

user184994
  • 17,791
  • 1
  • 46
  • 52
2

yes!just use innerHTML property by following:

<div [innerHTML]="model.property"></div>
0

I think you are talking about this

1.-HTML file add ngModelChange:

<div class="NgxEditor__Wrapper">
     <ngx-editor-menu [editor]="editor"> </ngx-editor-menu>
     <ngx-editor
         [editor]="editor"
         [(ngModel)]="html"
         [disabled]="false"
         [placeholder]="'Ingresa el texto...'"
         (ngModelChange)="editorChange($event)"
      ></ngx-editor>
</div>

2.-Componente file.

2.1.- Import the class toHtml:

import { Editor, toHTML } from 'ngx-editor';

2.2.-Create the function to catch the event:

  editorChange(event: any){
    const htmlTexEditor = toHTML(this.html);
    console.log(htmlTexEditor);
  }
0
extractContent(htmlCode: string) {
        let span = document.createElement('span');
        span.innerHTML = htmlCode;
        return span.textContent || span.innerText;
    };

this might be useful to someone

neekheel
  • 98
  • 2
  • 18