Tips for typing the HTML tag as text inside a span element with Angular

When retrieving the value "Evidence" from an API, it looks like "

<FORM METHOD="get" ACTION="search">
"

{
    data: {
        evidence:<FORM METHOD="get" ACTION="search">
    }
}

In my TypeScript file:

public evidence : any;
    
getEvidence(){
    this.service.getevidence().subscribe(data => {
        this.evidence= data.data.evidence                  // The value is stored here
    });

In the HTML file:

<span [innerHTML]="evidence"></span>

I would like to display the value but I am encountering the issue:

WARNING: sanitizing HTML stripped some content

Answer №1

To avoid sanitizing within the component itself,

app.component.ts :

import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

public information : any;

constructor(private _sanitizer:DomSanitizer) {}

bypassContent(value:string):SafeHtml {
    return this._sanitizer.bypassSecurityTrustHtml(value);
}

retrieveInformation(){
    this.service.getinformation().subscribe(data => {
        this.information= bypassContent(data.data.information);
    });
}

Alternatively, you can create a pipe for reusability purposes,

sanitizeHtml.pipe.ts :

import { Pipe, PipeTransform } from "@angular/core";
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({
  name: 'sanitizeHtml'
})
export class SanitizeHtmlPipe implements PipeTransform {

  constructor(private _sanitizer:DomSanitizer) {
  }

  transform(value:string):SafeHtml {
    return this._sanitizer.bypassSecurityTrustHtml(value);
  }
}

app.component.html :

<span [innerHTML]="information | sanitizeHtml"></span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Configuring the CKEditor edit functionality in Angular 2

If you're looking to configure your CKEditor in Angular2, you can refer to the documentation provided by CKEditor here. Here is an example of how I am using it in my HTML: <ckeditor [(ngModel)]="ckeditorContent" [config]="{toolbar : 'Bas ...

Issues encountered with Angular project when deploying on NGINX across various directories

I have two angular projects that I want to deploy on my nginx server. The first project is the main website, and the second is the admin portal. I aim to make the main site accessible at www.example.com, and the admin portal at www.example.com/admin. I ha ...

Incorporate a visual element into an Angular Library Component Template using an image asset

Currently, I am working with Angular 10 and aiming to develop a component library that includes images and stylesheets. My main goal is to be able to access these images from the component templates defined in the HTML template. Although I have limited ex ...

Retrieve an additional 10 items from the API when the button in the Angular list is clicked

I need to display 10 items each time the button is clicked. Below is the code snippet for the services: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http' @Injectable({ providedIn: ' ...

What are the reasons for being unable to utilize display flex within Angular?

Recently delving into Angular, I have been trying to apply display flex to <mat-form-field> without success. I have scoured Google for answers, but still haven't found a solution. Here is my HTML: <form [formGroup]="searchForm" (n ...

Managing the state of forms using NGRX and @Effects

After submitting a form and triggering an action that is caught by an effect for an http call, I am curious about how to handle the following scenarios upon completion or failure: Display a success message once the action finishes Reset all fields for fu ...

Is there a way for me to showcase a particular PDF file from an S3 bucket using a custom URL that corresponds to the object's name

Currently, I have a collection of PDFs stored on S3 and am in the process of developing an app that requires me to display these PDFs based on their object names. For instance, there is a PDF named "photosynthesis 1.pdf" located in the biology/ folder, and ...

Learn how to display data from the console onto an HTML page using Angular 2

I am working on a page with 2 tabs. One tab is for displaying active messages and the other one is for closed messages. If the data active value is true, the active messages section in HTML should be populated accordingly. If the data active is false, th ...

The Angular-cli is unable to link to the Bootstrap framework

Trying to reference the bootstrap.css file using Angular-cli. index.html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Title</title> <base href="/"> <link ...

Using TypeScript with Mongoose: Issue with finding documents conditionally on model results in error: Union type signatures are not compatible

Seeking assistance on how to conditionally access a mongoose model in TypeScript code. Does anyone have a solution to resolve this TypeScript error? Each member of the union type has signatures, but none of those signatures are compatible with each other ...

Limiting the image width of ngx-image-cropper to the popup container dimensions

I am currently working with a popup that contains an image cropper using ngx-image-cropper (https://www.npmjs.com/package/ngx-image-cropper) <div mat-dialog-container> <image-cropper [imageBase64]="imageFile" [mainta ...

Angular Typescript filter function returning an empty arrayIn an Angular project, a

I have a filtering function in Angular that is returning an empty array. Despite trying similar solutions from previous questions, the issue persists. The function itself appears to be correct. Any assistance would be greatly appreciated. gifts represents ...

"Twice the charm: Angular routing resolver runs not once

Having an angular 10 application with a routing resolver added to one of its components. Upon navigating to the resolver-added component through a different service, it has been noticed that the routing resolver is being called twice for each request. The ...

How to modify attributes using ng-content in Angular 2

Looking for a way to modify the attribute of the top div within the ng-content in my code. Here's an example snippet: <ng-container> <ng-content select="[content-body]"></ng-content> </ng-container> For instance, I want t ...

How to efficiently filter an array containing nested objects using TypeScript

I'm currently working with a list of menus and submenus: [ { "NUA_ID_Menu": 1, "TXT_Nom_Menu": "Menu 1", "Liste_Sous_Menus": [ { "TXT_Nom_Menu": ...

Is it feasible to securely remove an item from an array within an object without the need for any assertions in a single function?

My interest in this matter stems from curiosity. The title may be a bit complex, so let's simplify it with an example: type ObjType = { items: Array<{ id: number }>; sth: number }; const obj: ObjType = { sth: 3, items: [{ id: 1 }, { id: 2 } ...

Is it necessary to use an EventEmitter explicitly when performing two-way binding in Angular 2?

If I have a Kitchen class structured like this: @Component({ template: ` <kitchen [(kitchenLunch)]=lunch></kitchen> ` }) export class House { private lunch: Lunch; } The House component: Includes a sub-component Ki ...

Determine if the grid is accurately sorted in sequence using Cypress

I am currently working on sorting images based on their order, with the options for new and old. The [data-cy="list-file"] wraps all the images, while [data-cy=fileCard-list] represents each individual image. After selecting the 'old' b ...

What causes a hyperlink to take longer to load when using href instead of routerLink in Angular 2+?

As I work on my web application using angular 2 and incorporating routes, I noticed a significant difference in loading speed based on how I link to another component. Initially, being new to angular, I used the href attribute like this: <a href="local ...

Angular 4 is having trouble retrieving the JSON response

When I attempt to log errors in a specific scenario and use error, the following error message is displayed: { "errors" : [ { "entity" : "Movement", "property" : "direction", "invalidValue" : null, "message" : "Il campo non può essere v ...