How do i decode HTML entity in text using angular JS.
I have the string
""12.10 On-Going Submission of ""Made Up"" Samples.""
I need a way to decode this using Angular JS. I found a way to do that using javascript here but I am sure thats wont work for Angular. Need to get back the original string on the UI which would look like
""12.10 On-Going Submission of ""Made Up"" Samples.""
You can use the ng-bind-html
directive to display it as an html content with all the html entities decoded. Just make sure to include the ngSanitize
dependency in your application.
DEMO
JAVASCRIPT
angular.module('app', ['ngSanitize']) .controller('Ctrl', function($scope) { $scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."'; });
HTML
<body ng-controller="Ctrl"> <div ng-bind-html="html"></div> </body>
If you don't want to use ngSanitize, you can do it this way:
in your controller:
$scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."' $scope.renderHTML = function(html_code) { var decoded = angular.element('<textarea />').html(html_code).text(); return $sce.trustAsHtml(decoded); };
And in the template:
<div ng-bind-html="renderHTML(html)"></div>
Just make sure you inject $sce in your controller
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