Angular 2 has the possibility to make a string be interpreted as html?

Asked

Viewed 139 times

0

I have a variable called "link_youtube", which contains a data similar to this:

<iframe width="640" height="360" src="https://www.youtube.com/embed/eNs5g-c1Qno?list=RDeNs5g-c1Qno" frameborder="0" allowfullscreen></iframe>

however the browser does not identify it as an html, it identifies it as a string, need my system to identify it as an HTML, someone could help me?

follows below the html code:

insira o código aqui
<div class="text-center">
    <h1>{{sPCrtl.campanha.titulo}}</h1>
    {{sPCrtl.campanha.biografia_campanha.link_youtube}}">
</div>

1 answer

0

Try:

<div [innerHtml]="iframeYoutube"></div>

UPDATE

It really needs more stuff to insert an iframe, it had already tested with normal html, but iframe the angular blocks right away. Sorry for the shallow answer.

You need to inject the DomSanitizer angle to be able to inject your iframe:

iframeYoutube: SafeHtml;

Create a variable of type SafeHtml package @angular/platform-browser

Injects the DomSanitizer, also of the package @angular/platform-browser, in your Component builder:

constructor(
  ...
  private sanitizer: DomSanitizer,
  ...
) {}

And finally the moment you inject the iframe into the div do:

iframeYoutube = this.sanitizer.bypassSecurityTrustHtml(`<iframe width="560" height="315" src="youtube.com/embed/2dAY9dh-HWo"; frameborder="0" allowfullscreen></iframe>`);
  • Hello Giovane, I tried to use the command as follows <div [innerHtml]= "<iframe width="560" height="315" src="https://www.youtube.com/embeddAY9dh-HWo" frameborder="0" allowfullscreen></iframe>" - it did not work :/

  • tried to use, this.iframe = "<iframe width="560" height="315" src="https://www.youtube.com/embed/2dAY9dh-HWo" frameborder="0" allowfullscreen></iframe>" HTML: <div [innerHtml] = "{{Ctrl.iframe}}" = also did not work.

  • Should I import some dependency ?

  • @Matheusfreitas updated the answer, really I had not tested with iframe and I passed the answer because I had already injected html this way before. You’ll have a little more work to deal with iframe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.