I'm learning Angular and making a todo app. Creating a todo uses a wysiwyg editor (textAngular). It looks like this:
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:
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?
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.
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.
You can show HTML tags as plain text in HTML on a website or webpage by replacing < with < or &60; and > with > or &62; on each HTML tag that you want to be visible. Ordinarily, HTML tags are not visible to the reader on the browser.
<p class="text-muted" ng-bind-html="todo.description"></p>
https://docs.angularjs.org/api/ng/directive/ngBindHtml
you need to use $trustAsHTML
filter of $sce
(Strict contextual escaping)
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><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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With