Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

516
Visualizações
How can I display a SafeUrl or a SafeResourceUrl?

I'm loading a trusted URL into an iframe which works fine. I also want to display that URL as a string on the page. I've tried <div>{{url}}</div> but it displays:

SafeValue must use [property]=binding: /my/resource/path.html (see http://g.co/ng/security#xss)

I also tried using <div [ngModel]="url"></div>, but got

Error: No value accessor for form control with unspecified name attribute

How can I display it?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

  1. The error says use [property] binding - it means [innerHTML] for html
  2. You want to display this value as html (not url or resource) - use bypassSecurityTrustHtml

     @Component({
      selector: 'my-app',
      template: `
        <div [innerHTML]="url"><div>
      `
     })
    export class App {
    
      dangerousVideoUrl = "href='&#x3000;javascript:alert(1)'";
    
      constructor(private sanitizer: DomSanitizer) {
        this.url =
        this.sanitizer.bypassSecurityTrustHtml(this.dangerousVideoUrl);
     }
    }
    
about 4 years ago · Santiago Trujillo Relatório

0

You can do it in another way:

<iframe #foo [src]="contentUrl"></iframe>
<p>{{ foo.src }}</p>
about 4 years ago · Santiago Trujillo Relatório

0

Try with a @Pipe for using this in all your App and with DomSanitizer for sanitizing the URL and bypass XSS security.

@Pipe({
    name: 'sanitizeUrl',
    pure: false
})
export class AsString implements PipeTransform {
    constructor(private domSanitizer: DomSanitizer) {
    }

    transform(value: string, args?: any): any {            
        return this.domSanitizer.bypassSecurityTrustResourceUrl(value);
    }
}

And in your template:

<div>{{url | sanitizeUrl}}</div>
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda