繁体   English   中英

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

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

我知道可以使用 [( HTML [(ngModel)]="htmlContent" ,但是是否可以只获取文本? 谢谢。

例子:

这段文字是粗体 这是斜体

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

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

提出 github 问题

您可以使用DOMParser类解析它,然后只使用innerText属性。

假设你在一个名为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); 

有关解析器的更多信息, 访问https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML

是的!只需使用innerHTML属性:

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

我想你是在谈论这个

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.-组件文件。

2.1.- 将 class 导入到 Html:

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

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;
    };

这可能对某人有用

暂无
暂无

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

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