简体   繁体   English

以角度在打字稿文件中添加html标签

[英]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我正在使用 angular2,所以我想在 ts 文件的返回函数中实现 html 标签

  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试试这个代码stackblitz 示例

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

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

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