1

I was recently creating a web application using Angular 5.

I have built most of the thing but when I was required to add a <script> in my component template the code didn't work at compile time.

Can anybody suggest a solution?

html

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

component

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

When I used the developer options in Chrome, I found that display is none in the template component.

script {                       user agent stylesheet
    display: none;
}
Ayush Singhal
  • 21
  • 1
  • 6
  • Possible duplicate of [Need to insert Script tag in angular 2](https://stackoverflow.com/questions/42458346/need-to-insert-script-tag-in-angular-2) – The Hungry Dictator May 02 '18 at 05:18
  • I got how to insert script in angular the problem is that it is display: none on rendering in 'user agent stylesheet' – Ayush Singhal May 02 '18 at 18:41

0 Answers0