问题
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