Embed [src]="src" does not work in Angular 7, unsafe url

1

I am trying to create a PDF viewer in Angular but when passing it the variable gives error. Can you give me a hand, please?

HTML:

<embed [src]="selectedFile" class="p-col-12" style="height: 800px;">

TS:

  private pathFiles = [
    'src/assets/pdf/test.pdf',
    'src/assets/pdf/test2.pdf',
    'src/assets/pdf/test3.pdf',
  ];
  constructor() {
    public selectedFile = this.pathFiles[0];
  }

Thanks. Greetings!

    
asked by José Sánchez 07.12.2018 в 10:41
source

1 answer

1

Try using the DomSanitizationService service:

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

// ...

constructor(private sanador: DomSanitizationService) {
    public selectedFile = sanador.bypassSecurityTrustUr(this.pathFiles[0]);
}

For security reasons, [src] will only work if you secure the url to avoid url with information malisiona and that's what bypassSecurityTrustUr()

does     
answered by 07.12.2018 в 13:20