'How to escape HTML character in angularjs. I have read using ng-blind / ng-sanitize to escape Html, but not able to implement it
I am new to Angular Js. I have used ngx-editor to make text-area with different formatting Styles. When I am submitting the content I am getting HTML tags along with it. I know these tags are coming whenever I type content in the text-area and use different formatting styles, like to make content bold, italic, h1,h2,h3, etc.
I don't want these tags to display on the browser. Please provide me with a solution and explain why HTML tags are displaying on the browser along with the content.
Code Snippet
component.html
<div class = "NgxEditor__Wrapper" >
<ngx-editor-menu [editor]="editor" [toolbar]="toolbar"> </ngx-editor-menu>
<ngx-editor [editor]="editor" required placeholder="Write Content" formControlName="contentData"> </ngx-editor>
</div>
<mat-error *ngIf="blogForm.get('contentData').hasError('required')">
Description is Required!
</mat-error>
component.ts
import { Component, OnInit, OnDestroy, ViewEncapsulation } from '@angular/core';
import { AbstractControl, FormControl, FormGroup } from '@angular/forms';
import { Validators, Editor, Toolbar } from 'ngx-editor';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class AppComponent implements OnInit, OnDestroy {
editor = new Editor;
toolbar: Toolbar = [
['bold', 'italic'],
['underline', 'strike'],
['code', 'blockquote'],
['ordered_list', 'bullet_list'],
[{ heading: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] }],
['link', 'image'],
['text_color', 'background_color'],
['align_left', 'align_center', 'align_right', 'align_justify'],
];
ngOnInit(): void {
this.editor = new Editor();
}
ngOnDestroy(): void {
this.editor.destroy();
}
initializeForm(){
this.blogForm = this.fb.group({
contentData: ['', Validators.required],
});
} }
contentData: this.blogForm.value.contentData,
Input
Output:
<p><strong><span style="color:rgb(0, 0, 0);"><span style="background-
color:rgb(255, 255, 255);">Lorem</span></span></strong><span
style="color:rgb(0, 0, 0);"><span style="background-color:rgb(255, 255
Tags are also coming along with the output.
Solution 1:[1]
It's late to give an answer to this, but I was facing the same issue lately.
You have to bind the innerHtml property of a div to the editor content. Then the browser interprets it and it shows up properly formated like in the editor:
<div [innerHTML]="editor.html"></div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | AdrAs |