简体   繁体   中英

adding html tag inside typescript file in angular

i am using angular2 so i want to implement html tag inside the return function in ts file

  tooltip: (param: any) => {
        return `<span> ${param.value} </span>`;
      }

i have tried to use template literal but that make my span visible.

在此处输入图片说明

is there any idea how can i use the span proper way

try this code stackblitz example

import {Component, NgModule, Pipe, PipeTransform} from '@angular/core' import {BrowserModule} from '@angular/platform-browser' import { FormsModule } from '@angular/forms'; import { DomSanitizer } from '@angular/platform-browser' @Pipe({ name: 'safeHtml'}) export class SafeHtmlPipe implements PipeTransform { constructor(private sanitized: DomSanitizer) {} transform(value) { console.log(this.sanitized.bypassSecurityTrustHtml(value)) return this.sanitized.bypassSecurityTrustHtml(value); } } @Component({ selector: 'my-app', template: `<div [innerHtml]="tooltip('Hello World') | safeHtml"> </div>`, styleUrls: [ './app.component.css' ] }) export class AppComponent { name = 'Angular'; tooltip(param: any) { return `<span> ${param} </span>`; } }

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.

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