I am triying to output simples html unicode characters, for example ♣
from an expression.
I have tried to use ng-bind-html
and ng-bind-html-unsafe
but i can't make it appear in my HTML.
$scope.character = '♣';
<span ng-bind-html="{{character}}"></span>
<span ng-bind-html="♣"></span>
, i can see the HTML character correctly interpreted in my console, but still not injected into the span balise)How to output html characters from an expression ?
I import the script //ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-sanitize.min.js
and add the ngSanitize
dependency in my app.
You can enter any Unicode character in an HTML file by taking its decimal numeric character reference and adding an ampersand and a hash at the front and a semi-colon at the end, for example — should display as an em dash (—).
The text is displayed in HTML, so sometimes a LTR or RTL mark ( ‎ or ‏ ) is required to make 'weak characters' like punctuation display properly. These marks are not present in the source text due to technical limitations, so we need to add them in order for the final displayed text to appear correct.
To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.
The Unicode Standard has become a success and is implemented in HTML, XML, Java, JavaScript, E-mail, ASP, PHP, etc. The Unicode standard is also supported in many operating systems and all modern browsers.
You will have to use $sce (Strict Contextual Escaping), ngHtmlBindUnsafe was removed in 1.2
function myCtrl($scope,$sce){ $scope.html = $sce.trustAsHtml('♣'); }
Fiddle: http://jsfiddle.net/TheSharpieOne/uPw2U/
Furthernore, you can create a filter so that you will not need to escape everything in the controller.
.filter('html',function($sce){ return function(input){ return $sce.trustAsHtml(input); } })
HTML:
<span ng-bind-html="'♣'|html"></span>
Fiddle: http://jsfiddle.net/TheSharpieOne/uPw2U/1/
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