nCore
nCore

Reputation: 2087

better way of changing iframe url through DOM in Angular 2

I'm trying to replace a URL using (<HTMLInputElement>document.getElementById("workflow")).src however I don't think this is the right way of doing it.

Also tried the code below but its giving me getElementById of undefined.

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


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

Can someone point out how to do this properly in Angular 2?

Upvotes: 1

Views: 668

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657268

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

See also https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html

Upvotes: 1

Related Questions