9

I want to use iframes from youtube and other similar platforms - but I can't find out how to use a variable as my source.

I've searched for it already, but the answers I got didn't work out.

So here is the iframe:

<div class="right">
    <h2>Associated Pictures/Media Files</h2>
    <p>Here we will display either Infos, Fotos, Videos, Music or even interative content.</p>
    <div style="padding: 2%;">
        <iframe 
        width="100%" 
        height="315" 
        src="https://www.youtube.com/embed/Q7hmXN714RQ" 
        frameborder="0" 
        allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" 
        allowfullscreen>
        </iframe>
    </div>
</div>

the only thing I want is to use this variable here:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  source = 'https://www.youtube.com/embed/Q7hmXN714RQ';
}       

But when I try to do it in various ways - like for example:

src="{{source}}"

this doesn't work at all :(

Does anyone know how to do this right? I cannot find a proper solution for this problem.

1 Answer 1

19

What have you tried so far? Found this, maybe this works for you: https://stackblitz.com/edit/angular-youtube-video-in-iframe

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

2 Comments

Thanks, yes the DomSanitizer is what I was looking for.. However this stackblitz doesn't seem to work.. at least on my end it doesn't..
Alright, in my project at least the example worked, thanks a lot!

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.