23

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.

1
  • 1
    Why not simply use the unicode escape sequence: '\u2663'? Commented Jan 13, 2014 at 19:37

1 Answer 1

46

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.