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
This text is bold. This is italics
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
是的!只需使用innerHTML属性:
<div [innerHTML]="model.property"></div>
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);
}
extractContent(htmlCode: string) {
let span = document.createElement('span');
span.innerHTML = htmlCode;
return span.textContent || span.innerText;
};
this might be useful to someone
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.