Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output HTML unicode characters from an expression

Tags:

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>

  1. The html code is not injected into the span balise
  2. The html code is not interpreted (the html character doesn't appear in my console, but if i don't use an expression from a controller and directly call <span ng-bind-html="&clubs;"></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.

like image 433
Ludo Avatar asked Jan 13 '14 17:01

Ludo


People also ask

How do I show Unicode in HTML?

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 &#8212; should display as an em dash (—).

What is &LRM in HTML?

The text is displayed in HTML, so sometimes a LTR or RTL mark ( &lrm; or &rlm; ) 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.

How do you Unicode a character?

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.

Does HTML support Unicode?

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.


1 Answers

You will have to use $sce (Strict Contextual Escaping), ngHtmlBindUnsafe was removed in 1.2

function myCtrl($scope,$sce){     $scope.html = $sce.trustAsHtml('&clubs;'); } 

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="'&clubs;'|html"></span> 

Fiddle: http://jsfiddle.net/TheSharpieOne/uPw2U/1/

like image 127
TheSharpieOne Avatar answered Oct 23 '22 04:10

TheSharpieOne