Nandish
Nandish

Reputation: 125

angular2 custom modal component iframe issue

Having below code in modal

<iframe [src]="third/party/url.html"></iframe>

Upvotes: 1

Views: 2802

Answers (2)

Swanand Taware
Swanand Taware

Reputation: 753

For RC.6^ version use DomSanitizer

And a good option is using pure pipe for that:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer} from '@angular/platform-browser';

@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) {}
  transform(url) {
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }
} 

@NgModule({
   declarations : [
     ...
     SafePipe
   ],
})

html

<iframe width="100%" height="300" [src]="url | safe"></iframe>

For RC.5

You can leverage DomSanitizationService like this:

export class YourComponent {
  url: SafeResourceUrl;
  constructor(sanitizer: DomSanitizationService) {
    this.url = sanitizer.bypassSecurityTrustResourceUrl('your url');
  }
}

And then bind to url in your template:

<iframe width="100%" height="300" [src]="url"></iframe>

Don't forget to add the following imports:

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

Upvotes: 0

rohith
rohith

Reputation: 416

in angular2+ you have to do check this for security reasons,

in html

 <iframe class="e2e-iframe-untrusted-src" [src]="url"></iframe>

and in .ts file in

constructor(private sanitizer: DomSanitizer) { }

url=this.sanitizer.bypassSecurityTrustResourceUrl(res.data.url);

Upvotes: 2

Related Questions