0

Hi I'm trying to pass a javascript code through angular scope but on view page it renders as text. I also tried ng-Sanitize but it also didn't work.

<div id="xv-embed-13042533"></div> <script type="text/javascript"> (function() { var tn = document.createElement('script'); tn.type = 'text/javascript'; tn.async = true; tn.src = 'http://flashservice.example.com/embedcode/13042533/510/400/embed.js'; var s = document.getElementById('xv-embed-13042533'); s.parentNode.insertBefore(tn, s); })(); </script>

But on View page it renders as text in below format

<div id="xv-embed-13042533"></div> <script type="text/javascript"> (function() { var tn = document.createElement('script'); tn.type = 'text/javascript'; tn.async = true; tn.src = 'http://flashservice.example.com/embedcode/13042533/510/400/embed.js'; var s = document.getElementById('xv-embed-13042533'); s.parentNode.insertBefore(tn, s); })(); </script>

I'm using below syntax

<div id="Div2" ng-bind-html="htmltext2"></div> <br/>


1
  • Read the tag descriptions! Commented Jul 25, 2015 at 11:50

1 Answer 1

0

You should use $sce.trustAsHtml

$scope.unsafeHtml = $sce.trustAsHtml('<script>...<\/script>');

in conjunction with ng-bind-html

<div ng-bind-html="unsafeHtml"></div>

However, the above will not work, because jqLite doesn't handle <script> correctly. You have to resort to such workaround or load jQuery before Angular, so jQuery would be used as angular.element instead of jqLite.

Here's an example.

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

2 Comments

I have already created "sanitize" filter but that's not working
You didn't provide fully testable example. Here's a working one, it does exactly the same as the answer says plnkr.co/edit/iVre7ZZuBPZvY6XJcOvh?p=preview

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.