I laying out a styleguide with code examples within an AngularJS app.
In the past, I've used <pre>
. This no longer seems to work. I've tried it in codepen, and it still parses the HTML.
I do not want this.
<h1>I DO want this.</h1>
I'm guessing there's a filter to encode or decode the HTML, but I don't know which way I need to go. My intuition tells me I need to encode. However, everytime I try to google search a solution I get the opposite answers of what I want.
So for anyone who's better at googling than I am or know how to do this, please help.
Edit To appease the would be SO demigods, where's the code I ended up with...
This is how I started:
<xmp><button>Some example Button</button></xmp>
<xmp>
is deprecated, and has line break/whitespace issues. Couldn't use it.
Next one...
<pre><button>Some example Button</button></pre>
This sucked too, because it still parses the child nodes, meaning would get a button instead of a string.
What I ended up with:
// Template
<div class="sgForms__ex">
<div ng-bind-html="sgTrustHtml(buttons)"></div>
</div>
<div class="sgForms__code">
<div ng-bind="buttons"></div>
</div>
// Controller
angular.module('StyleGuide')
.controller('StyleGuideFormsController', ['$scope', '$sce', 'ListHelper',
function ($scope, $sce, listHelper) {
$scope.sgTrustHtml = function (i) {
return $sce.trustAsHtml(i);
};
$scope.buttons = '<button type="button" class="btn btn-primary">Call to Action</button>' +
'<button type="button" class="btn btn-default">Secondary CTA</button>'+
'<button type="button" class="btn btn-default">Inactive</button>';
}]);
I'm still running into an issue with line breaks. Note sure how I'm going to work around that.
Hello
"`, you will see angled brackets on the screen – Ruan Mendes Feb 12 '16 at 20:16