Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display HTML from model as HTML, not plain text

Tags:

html

angularjs

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?

like image 886
RobSeg Avatar asked Jul 29 '15 09:07

RobSeg


People also ask

Why is my HTML code showing up as text?

It is possible that the content is not html type. I made a similar mistake and was wondering why it is showing as text. Check the content of the file, most probably it is not HTML or some html tags must be missing. Make sure that Doc type is explicitly mentioned as HTML type at the begining of the document.

How do you display HTML code?

You can include code examples in your HTML by using the <code> tag. This automatically uses a monospaced font and also semantically labels our code as what it is.

How do I display HTML code without rendering?

You can show HTML tags as plain text in HTML on a website or webpage by replacing < with &lt; or &60; and > with &gt; or &62; on each HTML tag that you want to be visible. Ordinarily, HTML tags are not visible to the reader on the browser.


3 Answers

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

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

like image 159
Vishnu Avatar answered Oct 09 '22 17:10

Vishnu


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

like image 21
Subash Selvaraj Avatar answered Oct 09 '22 18:10

Subash Selvaraj


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>
like image 1
Christoph Avatar answered Oct 09 '22 18:10

Christoph