-1

I'm writing a web page and I use Angularjs to load HTML code saved in database but it just show like a string not active HTML code. How could I solve this problem? Thanks for your attention. This is my code in php file.

<td>{{uni.level_name}}</td>
<td>{{uni.entry_requirements}}</td>

And this is the problem. Problem when load HTML code saved in database

Anh Trinh
  • 157
  • 1
  • 3
  • 12
  • Hi, kindly go to View Source and show me how it was written there.. – Kasnady Oct 03 '16 at 03:09
  • Possible duplicate of [AngularJS : Insert HTML into view](http://stackoverflow.com/questions/9381926/angularjs-insert-html-into-view) –  Oct 03 '16 at 03:10
  • @AKZhang thanks for your reply, the source code from many people so I do not know where it was written. My task is just finding out the way to load HTML code from database and it will run normally not show original code like in the picture. Is there any way to do that? – Anh Trinh Oct 03 '16 at 03:25
  • @Terminus thanks for your help, that is not my problem because my HTML code saved and loaded from database not from any file in the project – Anh Trinh Oct 03 '16 at 03:27
  • `uni.entry_requirements` must be encoded with `http://php.net/manual/en/function.htmlspecialchars.php` or something similar. Is the source `<h4%gt;`? – chris85 Oct 03 '16 at 03:31
  • 1
    @JeongAe You're welcome! I think if you add the `trust` filter to your angular module and then mosify your html to look like so `{{uni.level_name | trust}} {{uni.entry_requirements | trust}}` your problem will be solved. The reason being, [angular escapes any html code you try to display automatically](https://docs.angularjs.org/api/ng/service/$sce) –  Oct 03 '16 at 03:35
  • @chris85 thanks for your answer, but I tried to use it and it's still not working. – Anh Trinh Oct 03 '16 at 07:37
  • @Terminus Thanks you very much! That's my problem and 'trust' can help me to solve that. In my situation, I use 'trust' like this http://stackoverflow.com/a/29225894/4758114 and I can solve the problem. – Anh Trinh Oct 03 '16 at 07:41
  • @JeongAe excellent! Just be aware of the danger of trusting user html (read about [cross site scripting](https://en.m.wikipedia.org/wiki/Cross-site_scripting). If the wiki isn't helpful, please do look around for another resource that explains it to your satisfaction) –  Oct 03 '16 at 10:52

1 Answers1

1

Use ng-bind-html:

<td ng-bind-html="uni.level_name"></td>
ystan-
  • 1,474
  • 1
  • 19
  • 43