6

I'm learning Angular and making a todo app. Creating a todo uses a wysiwyg editor (textAngular). It looks like this:

create todo with wysiwyg

This works properly and gets saved as html to ng-model named todo.description. But when displaying this todo, the html from the description is displayed as plain text. Like so:

Display todo HTML as plain text

The description is just bound to the model

<p class="text-muted">{{todo.description}}</p>

I think this can be done very simple but I didn't find a solution. I tried the xmp tag, but that's not what I'm looking for.

How can I make the description display HTML and not text?

4
  • probably this string is escaped. So you need to put raw text Commented Jul 29, 2015 at 9:09
  • possible duplicate stackoverflow.com/questions/17289448/… Commented Jul 29, 2015 at 9:15
  • 2
    Question is basically duplicate indeed. But the answer given by @Vishnu is different and beter. I consider this question not having added value, but the answer does. Commented Jul 29, 2015 at 9:20
  • @Tonny seems to be two different questions. In the mentioned links, all html tag will be ignored and model will be rendered as plain text without style. Commented Aug 5, 2016 at 3:28

3 Answers 3

7
<p class="text-muted" ng-bind-html="todo.description"></p>

https://docs.angularjs.org/api/ng/directive/ngBindHtml

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

Comments

1

you need to use $trustAsHTML filter of $sce (Strict contextual escaping)

Comments

1

You can use ngBindHtml to display your html code.

var app = angular.module('myApp', ['ngSanitize']);

app.controller('myCtrl', ['$scope',
  function($scope) {
    $scope.html = '<b>Test</b><u>Test</u>&lt;HTML';
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-sanitize.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <span ng-bind-html="html"></span>
</div>

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.