Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

508
Views
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 answers
Answer question

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 Report

0

You can do it in another way:

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!