3

I got the code from another question and it's straightforward and working fine

<div ng-controller="ExampleController">
<p ng-bind-html="testHTML"></p>
(function(angular) {
  'use strict';
  angular.module('bindHtmlExample', ['ngSanitize'])
  .controller('ExampleController', ['$scope', function($scope) {
       $scope.testHTML = 'I am an <code>HTML</code>string with ' +
                         '<a href="#">links!</a> and other <em>stuff</em>';
  }]);
})(window.angular);

Suppose, I'm getting an object and I want to show an element of the object

var obj = {
   title: 'Title',
   description: 'I am an <code>HTML</code>string with ' +
       '<a href="#">links!</a> and other <em>stuff</em>'
};
$scope.testHTML = obj;

Then how should I bind only the description on the html end? I've tried <p ng-bind-html="{{testHTML.description}}"></p>and <p ng-bind-html="testHTML.description"></p>

plnkr example

1
  • Did you assign $scope.testHTML = obj, then var obj = {...} in your code ? Commented Sep 8, 2016 at 2:59

3 Answers 3

2

Try this snippet to bind HTML

Firstly, you can create custom filter to bind the HTML in AngularJS.

Now, you can use this filter anywhere into the myApp module by just writing yourHTML | html, That will done the job.

var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope','$sce', function($scope, $sce) {
  var obj = {
     title: 'Title',
     description: 'I am an <code>HTML</code>string with ' +
       '<a href="#">links!</a> and other <em>stuff</em>',
     description1: 'I am an <b>HTML</b>string with ' +
       '<a href="#">links!</a> and other <u><b>stuff</b></u>'
    
  };
  $scope.testHTML = obj;
  
  //Use $SCE in controller
  var preview_data = obj.description;
  $scope.htmlSafe = $sce.trustAsHtml(preview_data);
  
}]);
//Custom filter (Alternate way)
myApp.filter('html', ['$sce', function ($sce) { 
    return function (text) {
        return $sce.trustAsHtml(text);
    };    
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="GreetingController">
  
  <b>By using custom filter</b>
  <div ng-bind-html="testHTML.description | html"></div>
  <div ng-bind-html="testHTML.description1 | html"></div>

  <br/>
  <b>By using $SCE inside controller</b>
  <div ng-bind-html="htmlSafe"></div>

</div>

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

6 Comments

Console's saying $sce.trustAsHTML is not a function
I've created the snippet that is working, if you can check that. or provide me your jsfiddle.
Which version of Angular Js you're using ? it should be 1.2 at least
@SagnikChakraborti I've updated my answer and added two ways to do this.
Thank You. It's working fine now. Had a problem in my code. However, with ngSanitize iframe wasn't showing. I don't know why but I'm curious. Now it's showing perfectly. :)
|
0

in plnkr example of html file:

change <p ng-bind-html="{{testHtml.desc}}"></p>to <p ng-bind-html="testHtml.desc"></p> as below:

<body ng-app="bindHtmlExample">
  <div ng-controller="ExampleController">
 <p ng-bind-html=testHtml.desc></p>
</div>

Comments

0

ng-bind-html doesn't require interpolation as it accepts an expression, so you only need to set it to testHtml.desc without the curly braces {{}}.

In your plunkr's controller you're assigning obj to $scope.testHtml before you declare obj itself, so it won't render anything since obj is undefined at assignment.

Plunkr: https://plnkr.co/edit/GvujCcYdCqLvwwnuEnWp?p=preview

Comments

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.