简体   繁体   English

有没有办法从 ngx-editor 获取文本?

[英]Is there a way to get text from the ngx-editor?

I am aware that HTML can be retrieved using [(ngModel)]="htmlContent" , but is it possible to get just the text?我知道可以使用 [( HTML [(ngModel)]="htmlContent" ,但是是否可以只获取文本? Thanks.谢谢。

Example:例子:

This text is bold .这段文字是粗体 This is italics这是斜体

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

text content: This text is bold. This is italics文字内容: This text is bold. This is italics This text is bold. This is italics

Raised github issue提出 github 问题

You can parse it with the DOMParser class, and then just use the innerText property. 您可以使用DOMParser类解析它,然后只使用innerText属性。

Assuming you have the HTML in a variable called html , it would be 假设你在一个名为html的变量中有html ,那就是

 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 有关解析器的更多信息, 访问https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML

是的!只需使用innerHTML属性:

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

I think you are talking about this我想你是在谈论这个

1.-HTML file add ngModelChange: 1.-HTML 文件添加 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.-组件文件。

2.1.- Import the class toHtml: 2.1.- 将 class 导入到 Html:

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

2.2.-Create the function to catch the event: 2.2.-创建 function 来捕获事件:

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

this might be useful to someone这可能对某人有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM