【问题标题】:better way of changing iframe url through DOM in Angular 2在Angular 2中通过DOM更改iframe url的更好方法
【发布时间】:2016-09-16 10:06:28
【问题描述】:

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

还尝试了下面的代码,但它给了我未定义的 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 中正确执行此操作吗?

【问题讨论】:

    标签: javascript angularjs iframe angular


    【解决方案1】:
    <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

    【讨论】:

    • 谢谢,这个答案。
    猜你喜欢
    • 2011-12-29
    • 2016-02-17
    • 1970-01-01
    • 2023-04-05
    • 2017-09-30
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多