javascript - 在 Angular 2 中通过 DOM 更改 iframe url 的更好方法

标签 javascript angularjs iframe angular

我正在尝试替换 URL使用(<HTMLInputElement>document.getElementById("workflow")).src但我认为这不是正确的做法。

还尝试了下面的代码,但它给了我 getElementById未定义。

import { Component, ElementRef, Inject } from '@angular/core';
constructor(private elementRef: ElementRef){}


 this.elementRef.nativeElement.document.getElementById("workflow").src = "./javascript/workfloweditor.html?title="+workFlowTitle;

有人可以指出如何在 Angular 2 中正确执行此操作吗?

最佳答案

<iframe [src]="iframeSrc">
constructor(private sanitizer:DomSanitizer) {
}

get iframeSrc() {
  this.sanitizer.bypassSecurityTrustUrl('./javascript/workfloweditor.html?title='+workFlowTitle);
  // or
  this.sanitizer.bypassSecurityTrustResourceUrl('./javascript/workfloweditor.html?title='+workFlowTitle);
}

另请参阅https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html

关于javascript - 在 Angular 2 中通过 DOM 更改 iframe url 的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39528820/

相关文章:

javascript - jquery .height() 在媒体查询中使用百分比高度时不起作用

javascript - 在同一页面上添加多个 Google Places 实例

javascript - 在客户端 Angularjs 上解压包含文件的文件夹作为内容

php - 如何将 php session 变量从页面传输到页面内的 iframe

html - Firefox 中的 iframe 调整大小问题

javascript - 如何在第 n 个分隔符处将表列拆分为新列

javascript - 删除 Unicode 代码的转义

javascript - Angular : Google maps directive loaded in another Modal directive stays blank

java - Selenium - 等待页面渲染动态页面

javascript - 使用 javascript 将 iframe contenteditable 中的内容绘制到 html5 Canvas 上