如何在Angular 5组件模板中使用脚本标记

时间:2018-05-02 05:13:51

标签: angular typescript angular5 angular2-template

我最近使用Angular 5创建了一个Web应用程序。

我构建了大部分内容但是当我需要在组件模板中添加<script>时,代码在编译时不起作用。

有人可以建议解决方案吗?

HTML

<section class="col span-2-of-6 ads">
    <div [innerHtml]="html | safe: 'html'" ></div>
</section>

组件

html : string = "<script type='text/javascript' language='javascript' src=''></script>";     //ads js src in src=""

safe.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

@Pipe({
  name: 'safe'
})
export class SafePipe implements PipeTransform {

  constructor(protected sanitizer: DomSanitizer) {}

  public transform(value: any, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
    switch (type) {
      case 'html': return this.sanitizer.bypassSecurityTrustHtml(value);
      case 'style': return this.sanitizer.bypassSecurityTrustStyle(value);
      case 'script': return this.sanitizer.bypassSecurityTrustScript(value);
      case 'url': return this.sanitizer.bypassSecurityTrustUrl(value);
      case 'resourceUrl': return this.sanitizer.bypassSecurityTrustResourceUrl(value);
      default: throw new Error(`Invalid safe type specified: ${type}`);
    }
  }
}

当我在Chrome中使用开发者选项时,我发现模板组件中displaynone

script {                       user agent stylesheet
    display: none;
}

0 个答案:

没有答案