Angular2中的iframe

时间:2017-02-03 16:38:09

标签: angular

从@ angular / core';

导入{Component}
@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';

}

我有上面提到的.ts文件。如何将testRequestId传递给html。

2 个答案:

答案 0 :(得分:11)

试试这个:

Online demo

安全管道

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';

}

因为Angular不信任任何来源,它会对内容进行清理,然后我们需要绕过它。

详细了解此主题:https://angular.io/docs/ts/latest/guide/security.html

Template syntax

答案 1 :(得分:1)

只需将+[testRequestId]+更改为{{testRequestId}}

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';
}