0

I am working on a project based on Angular 2. I got stuck in one problem. I am dealing with Iframe. In my angular component, I am generating an Iframe as:

    this.ifrm = document.createElement("iframe");
    console.log(title_colorr);
    this.ifrm.setAttribute("src", "http://web.com/newsfeed/");
    this.ifrm.style.width = widthh + "px";;
    this.ifrm.style.height = heightt + "px";

I am getting a full Iframe code when I am putting

console.log(this.ifrm); as: 

<iframe src="http://web.com/newsfeed/" style="width: 250px; height: 250px;"></iframe>

Now the problem is when I am trying to use this in my HTML template as:

<p >{{ifrm}}</p> then I am getting :

[object HTMLIFrameElement]

I have used <p [innerHTML]="ifrm"></p> too, but no solution is there.

Is there anybody, who knows how to solve this?

2

1 Answer 1

0

Import ElementRef and Renderer2 from '@angular/core' and change your constructor to

  constructor(private elementRef: ElementRef, private renderer: Renderer2) { }

Now you can use appendChild function of renderer as follows:

this.renderer.appendChild(this.elementRef.nativeElement, this.ifrm);

In your html file you can just have

<p></p>

And it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.