如何从表达式输出HTML unicode字符

Lud*_*udo 23 angularjs

我正在试图输出简单的html unicode字符,例如♣从表达式输出.

我试图使用ng-bind-html,ng-bind-html-unsafe但我不能让它出现在我的HTML中.

$scope.character = '♣';

<span ng-bind-html="{{character}}"></span>

  1. html代码没有注入span balise
  2. html代码没有被解释(html字符没有出现在我的控制台中,但如果我不使用控制器中的表达式并直接调用<span ng-bind-html="&clubs;"></span>,我可以在我的控制台中看到正确解释的HTML字符,但仍未注入进入跨度应答器)

如何从表达式输出html字符?

我导入脚本//ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-sanitize.min.jsngSanitize在我的应用程序中添加依赖项.

The*_*One 46

你将不得不使用$ sce(Strict Contextual Escaping),在1.2中删除了ngHtmlBindUnsafe

function myCtrl($scope,$sce){
    $scope.html = $sce.trustAsHtml('&clubs;');
}
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/TheSharpieOne/uPw2U/

此外,您可以创建一个过滤器,这样您就不需要转义控制器中的所有内容.

.filter('html',function($sce){
    return function(input){
        return $sce.trustAsHtml(input);
    }
})
Run Code Online (Sandbox Code Playgroud)

HTML:

<span ng-bind-html="'&clubs;'|html"></span>
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/TheSharpieOne/uPw2U/1/