Is there a way to configure ng-bind-html that it renders a string like '</'?

469 views Asked by At

it seems ng-bind-html is rendering '</' like a close tag, so it has been sanitized, and the string has pass through, but it renders as close tag in html, which means nothing shows. for example, 'I find </ is a good thing', if I bind the string to it, everything after </ will be cut off, it only shows 'I find' in the web page

(add on) The problem is actually we were mix-using html encoding and url encoding at the same time, right now, the input from front end is like '</ is good', then there is a service to encode it, after encoding, it becomes '&lt%2F is good', before it gets into database, it becomes '&lt/ is good', and when we get data back, ng-bind-html picks it up as '</ is good'. but we have an input field to edit it that's using ng-model, so ng-model picks it up as '$lt/ is good', which is the same as database. so before we edit it, we do a replace() function in the controller, so we temporarily fixed the problem. But still not sure if it's the right way, even in stackoverflow itself, the similar problem exists, you can see the </ has a grey background, because stackoverflow rich text editor doesn't take it as a normal string, so I made backtick escapes for this particular string to show in the text editor.

1

There are 1 answers

1
georgeawg On

Changing '<' to '&lt;' works as expected:

angular.module("app",[])
.controller("ctrl", function($scope,$sce) {
    $scope.text = "I find &lt;/ is a good thing";
    $scope.trust = $sce.trustAsHtml($scope.text);
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
   <h2>Using ng-bind-html</h2>
   <p ng-bind-html="trust"></p>
   <h2>Using ng-bind</h2>
   <p ng-bind="text"></p>
</body>

For more information, see