0

I want to bind iframe in a div that is returning by my database,

<iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/zvo9m_e8ekA&; frameborder=&quot;0&quot; allow=&quot;autoplay; encrypted-media&quot; allowfullscreen></iframe>

Is this possible to bind in following div

<div ng-app="myApp" ng-controller="dummy">
3
  • are you returning it as string? You might need ng-bind-html Commented May 29, 2018 at 13:31
  • Yes, I'm returning it as string. I've already tried ng-bind-html but it's not working Commented May 29, 2018 at 13:34
  • you need to explain why it's not working. Did you inject the ngSanitize module? Have you escaped quotes in your string? Do you have any errors in the console? Commented May 29, 2018 at 13:37

2 Answers 2

1

You will need to use the $sce service in conjunction with ng-bind-html in order to parse the string as safe HTML.

If your string is: "<iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/zvo9m_e8ekA&; frameborder=&quot;0&quot; allow=&quot;autoplay; encrypted-media&quot; allowfullscreen></iframe>"

Then you need to replace the &quot; with apostrophes and then parse it as safe HTML, as such:

dummy.unsafeIframeCode = "<iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/zvo9m_e8ekA&; frameborder=&quot;0&quot; allow=&quot;autoplay; encrypted-media&quot; allowfullscreen></iframe>";

dummy.unsafeParsedIframeCode = dummy.unsafeIframeCode.replace(/\&quot\;/gi, "'");

dummy.safeIframeCode = $sce.trustAsHtml(dummy.unsafeParsedIframeCode);

And simply bind it in HTML as such:

<div ng-app="myApp" ng-controller="dummy" ng-bind-html="dummy.safeIframeCode">

Full working JSFiddle here

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

1 Comment

Thanks a lot Protozoid
0

ng-bind-html willn't work for binding your iFrame, because the sanitizer is protecting your app from potential XSS attacks.

If you trust/control the data source completely, you can look into using trustAsHtml e.g. scope.trustedData = $sce.trustAsHtml(content); in your directive controller(where content is your iFrame "<iframe.../>"), and <div ng-bind-html="trustedData"></div> in the DOM. It'll do the task for you.

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.