2

The code:

var appModule = angular.module('lookbookApp',
    [
        'ngSanitize',
    ])

appModule.filter('to_trusted', ['$sce', function ($sce) {
    return function (text) {
        return $sce.trustAsHtml(text);
    };
}]);

<div class="text pre-txt" ng-bind-html="caseStudy.Overview | to_trusted"></div>

The content of caseStudy.Overview:

<ul>
    <li>aaaa</li>
    <li>bbbb</li>
    <li>cccc</li>
    <li>dddd</li>
</ul>

The expected output:

<ul>
  <li>aaaa</li>
  <li>bbbb</li>
  <li>cccc</li>
  <li>dddd</li>
</ul>

I've read articles on internet devoted to this issue, but my unordered list is still displayed as:

   aaaa
   bbbb
   cccc
   dddd

How to resolve this issue?

tesicg
  • 3,971
  • 16
  • 62
  • 121

2 Answers2

0

If I understood you correctly, then you want to render HTML instead of rendered text.

If that's the case, then you shouldn't be using ng-bind-html

ng-bind-html
ng-bind-html renders the html instead of showing html tag.

See the difference here.


Use this code

<!DOCTYPE html>
<html ng-app="lookbookApp">

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
    <script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
    <script src="script.js"></script>
    <script src="https://code.angularjs.org/1.5.8/angular-sanitize.js"></script>
  </head>

  <body ng-controller="myCtrl">
    <h1>Hello Plunker!</h1>
    <div class="text pre-txt" ng-bind="caseStudy.Overview | to_trusted"></div>
    
    <script>
      
      var appModule = angular.module("lookbookApp", ['ngSanitize']);

      appModule.controller("myCtrl", ['$scope', function($scope) {
        $scope.test = "Hello World";
        $scope.caseStudy = {};
        $scope.caseStudy.Overview = '<ul><li>aaaa</li><li>bbbb</li><li>cccc</li><li>dddd</li></ul>';
      }]);
      
      
      appModule.filter('to_trusted', ['$sce', function ($sce) {
          return function (text) {
              return $sce.trustAsHtml(text);
          };
      }]);


      
    </script>
  </body>

</html>

Output

<ul><li>aaaa</li><li>bbbb</li><li>cccc</li><li>dddd</li></ul>
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
-2

You code works well.

$scope.caseStudy = {
    "Overview": "<ul><li>aaaa</li><li>bbbb</li><li>cccc</li><li>dddd</li></ul>"
};    

Have a look at the demo.

Shashank
  • 2,010
  • 2
  • 18
  • 38