Angular 如何实例化DomsanizationService

Angular 如何实例化DomsanizationService,angular,Angular,我需要从我的url:unsafe:blob:中删除unsafe,下面是我所做的 脚本: import {DomSanitizationService} from '@angular/platform-browser'; class example{ public DomSanitizer: DomSanitizationService; public url; constructor() { this.url = 'www.example.com'; } } HTM

我需要从我的url:unsafe:blob:中删除unsafe,下面是我所做的

脚本:

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

class example{
  public DomSanitizer: DomSanitizationService;
  public url;

  constructor() {
    this.url = 'www.example.com';
  }
}
HTML:

),DomsanizationService是从构造函数传递的,但我不明白在实例化该类时应该传入什么。另外,我不想改变建造商的合同。我想知道实现我的目标的正确方法是什么。我正在使用angular2

更新: 现在我可以通过构造函数注入DomsanizationService使其工作,但是,在我的测试中,
我需要实例化我的组件,以及应该为domsanizationservice传递什么?

需要通过构造函数注入
domsanizizer

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

class example{ 
  public url;

   constructor(private sanitized: DomSanitizer) {

      this.url = 'www.example.com';
   }

   transform() { 
      return this.sanitized.bypassSecurityTrustHtml(this.url);
   }

}

<a href={{transform()}}>
从'@angular/platform browser'导入{domsanizationservice};
类示例{
公共网址;
构造器(专用消毒:DOMSanizizer){
this.url='www.example.com';
}
转换(){
返回this.sanitized.bypassSecurityTrustHtml(this.url);
}
}

您必须在组件中注入
DOMSanizer
服务

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


export class Example {
    htmlData:SafeHtml
    constructor(private sanitizer: DomSanitizer) {
       this.url = 'www.example.com';
    }

    renderTab() {
       let temp=`<a href="${this.url}">`
                this.htmlData=this.sanitizer.bypassSecurityTrustHtml(temp);
            }

        }
    }
从“@angular/platform browser”导入{domsanizer,SafeHtml};
导出类示例{
htmlData:SafeHtml
构造函数(专用消毒器:DomSanitizer){
this.url='www.example.com';
}
renderTab(){
让临时工=``
this.htmlData=this.sanitizer.bypassSecurityTrustHtml(临时);
}
}
}
}

在html中使用这个

<span [innerHTml]="htmlData"></span>

处理它的最佳方法是创建一个消毒器管道。稍后,您将只添加类似于
数据| sanitize:'html'
的内容,而不是复制功能

角度文档->


管道创建示例。

我想这个问题已经在这里得到了回答:检查上面的线程,它有您正在寻找的答案
<span [innerHTml]="htmlData"></span>