2016-10-04 21 views
12

我一直在使用DomSanitizer和一個html字符串中的SVG。Angular 2,DomSanitizer,bypassSecurityTrustHtml,SVG

此之前的角當前的版本中,這個工作就好了:

this.domSanitizer.bypassSecurityTrustHtml(content);

現在我又回到叫

SafeHtmlImpl {changingThisBreaksApplicationSecurity: "<svg> blah </svg>"} 
changingThisBreaksApplicationSecurity 

對象現在是否有訪問的新途徑DomSanitizer的輸出?我應該將它作爲SafeHTML類型還是什麼?如果bypassSecurityTrustHtml仍然過濾HTML,那麼它有什麼意義?

任何明信片上的答案?請...

+0

那麼,有什麼問題呢? – micronyks

回答

23

DEMO:https://plnkr.co/edit/Qke2jktna55h40ubUl8o?p=preview

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]="html | safeHtml"> 
    </div> 
    `, 
}) 
export class App { 
    name:string; 
    html: safeHtml; 
    constructor() { 
    this.name = 'Angular2' 
    this.html = "<svg> blah </svg>"; 
    } 
} 
+0

我已將您的svg更改爲其他svg以使其正常工作。 – micronyks

+0

謝謝。關於管道的好主意,我已經設置好了,並且工作。實際上沒有做任何功能上的不同,但誰在抱怨?有用。 – Tom

+0

誰在抱怨? – micronyks