如何在函数内使用DomSanitizer [英] How to use DomSanitizer inside a function

查看:93
本文介绍了如何在函数内使用DomSanitizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在Triyin中构建可重用的功能,并清理了一些内容,但我不知道要调用 DomSanitizer 还是会给我错误,它是抽象类.这是我的功能:

Hi I´m triyin to build a function to be reusable, and sanitize some content, but I don´t know hoe to call DomSanitizer it allways give me the error that is and abstract class. Here is my function:

export function PostFormat(post){
  let sanitizer: DomSanitizer; // TODO!

  post['title'] = sanitizer.bypassSecurityTrustHtml(post['title']['rendered']);
  post['author'] = post['_embedded']['author'][0];
  post['content'] = sanitizer.bypassSecurityTrustHtml(post['content']['rendered']);
  post['excerpt'] = sanitizer.bypassSecurityTrustHtml(post['excerpt']['rendered']);

  if (post['_embedded']['wp:featuredmedia']){
    if (post['_embedded']['wp:featuredmedia'][0]['media_details']){
      post['featured_image'] = post['_embedded']['wp:featuredmedia'][0]['media_details']['sizes'][this.default_size]['source_url'];
    }
  }
  if (post['_embedded']['replies']){
    post['comments'] = post['_embedded']['replies'][0];
  }
  return post;
}

推荐答案

使用它的简便方法是添加导入

Easy way to use it is add import

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

之后,您可以使用消毒剂的所有功能

after that you can use all funcs of sanitizer

func trueHTML(post) {
  return this.sanitizer.bypassSecurityTrustHtml(
    post.replace(
      regex,
      match => `<a target="_blank" href="${match}">${match}</a>`
    )
  );
}

并添加 inner html

<div [innerHtml]="post"></div>

这篇关于如何在函数内使用DomSanitizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆