Rajesh Narravula
Rajesh Narravula

Reputation: 1463

html tags not displaying using ng-bind-html in Angularjs

I have html template like this:

I want to bind this template using "ng-bind-html", like below:

angular.module('bindHtmlExample', ['ngSanitize'])
  .controller('ExampleController', ['$scope', '$compile',
    function($scope, $compile) {
      var templateHTML =
        'I am an <code>HTML</code>string with ' +
        '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()"></i></span>';
      $scope.myHTML = $compile(templateHTML)($scope);
    }
  ]);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.3/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.3/angular-sanitize.js"></script>

<div ng-app="bindHtmlExample">
  <div ng-controller="ExampleController">
    <p ng-bind-html-unsafe="myHTML"></p>
    <p ng-bind-html="myHTML"></p>
  </div>
</div>

nothing I'm getting. How to fix this.

Upvotes: 0

Views: 3423

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388436

I think a possible solution here is to write your own directive like

angular.module('bindHtmlExample', ['ngSanitize'])
    .controller('ExampleController', ['$scope', '$compile', function ($scope, $compile) {
    var templateHTML =
        '<div>I am an <code>HTML</code>string with ' +
        '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()">i</i></span></div>';
    $scope.myHTML = templateHTML;

    $scope.refresh = function () {
        console.log('refresh')
    };
}]);

angular.module('bindHtmlExample').directive('myHtml', ['$compile', function ($compile) {
    return {
        restrict: 'A',
        link: function link($scope, $element, attrs) {
            attrs.$observe('myHtml', function (value) {
                var $el = $compile(value)($scope);
                $element.empty().append($el)
            })
        }
    }
}])
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.3/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.3/angular-sanitize.js"></script>

<div ng-app="bindHtmlExample">
    <div ng-controller="ExampleController">
        <p ng-bind-html-unsafe="myHTML"></p>
        <p ng-bind-html="myHTML"></p>
        <p my-html="{{myHTML}}"></p>
    </div>
</div>

Upvotes: 2

Jennie Ji
Jennie Ji

Reputation: 809

When you use $complie it will return a jqLite object, not a HTML string. However, the value of the variable for ngBindHTML should be a string including HTML. That's why you can see nothing as result.

For your situation, it's better to use Directive than Controller to insert your HTML. See my Codepen: http://codepen.io/jennieji/pen/jhnCk

JS:

  angular.module('bindHtmlExample', ['ngSanitize'])
  .controller('ExampleController', ['$scope', '$interpolate',
    function($scope, $interpolate) {
      var templateHTML =
        'I am an <code>HTML</code>string with ' +
          '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()">{{ bindText }}</i></span>';
      $scope.bindText = 'Refresh()';
      $scope.refresh = function() {
        console.log('refresh() runned.');
      };
      $scope.interpolate = $interpolate(templateHTML)($scope);
    }
  ])
  .directive('exampleDirective', ['$compile', function($compile){
    return function(scope, element){
       var templateHTML =
        '<p>I am an <code>HTML</code>string with ' +
          '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()">{{ bindText }}</i></span></p>';
       element.append( $compile(templateHTML)(scope) );
    };
  }]);

HTML:

    <div ng-app="bindHtmlExample">
      <div ng-controller="ExampleController" example-directive>
        <p ng-bind-html-unsafe="interpolate"></p>
        <p ng-bind-html="interpolate"></p>
      </div>
    </div>

Upvotes: 0

alecxe
alecxe

Reputation: 474171

According to the Vojta's comment in this issue:

qLite throws when given a string that does not start with "<", it should trim the string first.

In other words, your HTML string have to be an "element" with an opening and closing tags.

Put the HTML string you have into a div container:

var templateHTML =
    '<div>I am an <code>HTML</code>string with ' +
    '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()"></i></span></div>';

Also see:

Upvotes: 1

Related Questions