Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

509
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

You can do it in another way:

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda