Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iframes in Angular2

Tags:

angular

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>
             <iframe src="http://hssa:1021/Home?testRequestId=+[testRequestId]+" allowfullscreen></iframe>`,
})
export class AppComponent  {
    name = 'Angular';
    testRequestId = '3224'; 
}

I have my .ts file as mentioned above. How do I pass testRequestId to html?

like image 707
test Avatar asked Feb 03 '17 16:02

test


1 Answers

try this:

Online demo

Safe Pipe

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: string) {
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }
}

AppComponent

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <iframe [src]="'https://www.youtube.com/embed/' + testRequestId | safe" width="560" height="315" allowfullscreen></iframe>
  `
})
export class AppComponent {
  testRequestId: string = 'uelHwf8o7_U';

}

because Angular not trust any source, it'll sanitize the content, then we need bypass it.

Learn more about this topic: https://angular.io/docs/ts/latest/guide/security.html

Template syntax

like image 143
Tiep Phan Avatar answered Oct 15 '22 00:10

Tiep Phan