admin管理员组文章数量:1310160
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
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
Share Improve this question edited Jan 7, 2020 at 12:55 R. Richards 25.2k10 gold badges66 silver badges65 bronze badges asked Jan 7, 2020 at 12:54 A.AlshaikhliA.Alshaikhli 4374 gold badges9 silver badges19 bronze badges 2-
You have to use
DomSanitizer
with the functionbypassSecurityTrustHtml
– Jacopo Sciampi Commented Jan 7, 2020 at 12:55 -
Use innerHTML
<div [innerHTML]="tooltip(param)'"></div>
Can you share more code as Calling methods in bindings in not remended as it would be invoked in every change detection cycle Instead use a property and bind it in inner html. – Vikas Commented Jan 7, 2020 at 12:57
1 Answer
Reset to default 3try 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.ponent.css' ] }) export class AppComponent { name = 'Angular'; tooltip(param: any) { return `<span> ${param} </span>`; } }
本文标签: javascriptadding html tag inside typescript file in angularStack Overflow
版权声明:本文标题:javascript - adding html tag inside typescript file in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741849242a2400962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论