3

I have a div tag in my view:

<div ng-bind-html="investmentVm.item.embed_code"></div>

And in my controller, I get script tag from database and put it in to model:

this.item.embed_code = this.$sce.trustAsHtml("<script>alert('OK');</script>");

If I put a html tag at the end of embed_code, it can be displayed by ng-bind-html, but script tag does not display.

In the old version of AngularJS we can use ng-bind-html-unsafe to bind a script tag but it has been removed in new version.

How can I add a script tag and run javascript code inside just like:

<script type="text/javascript">(function(n){
     //Do something here
});</script>
5
  • @webeno Sorry, I accidentally deleted your comment. The script stored in database, I call to API and get it inside a json object just like : item = { name: "Script name", owner: "quangtran", embed_code: "<script type="text/javascript">(function(n){ //Do something here});</script>" } Commented Apr 8, 2016 at 12:15
  • 1
    I guess you meant my edit, right? no worries, you can see it in history, hope you can learn from it... ;) Commented Apr 8, 2016 at 12:18
  • and if you have clarification, add it in the question, I may not be the one answering it... Commented Apr 8, 2016 at 12:27
  • 1
    possible duplicate of stackoverflow.com/questions/13711735/… Commented Apr 8, 2016 at 12:37
  • Thank you for your comments, I have resolved this issue by writing that embed-code into a template and load it into an iframe. This is not a good solution but it work fine for my case :) Commented Apr 12, 2016 at 3:56

0

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.