How to use DomSanitizer inside a function

人走茶凉 提交于 2021-01-28 10:30:26

问题


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

回答1:


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

and add your text like inner html

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


来源:https://stackoverflow.com/questions/46221578/how-to-use-domsanitizer-inside-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!