如何从一个前pression输出HTML UNI code字 [英] How to output HTML unicode characters from an expression
问题描述
我triying输出simples HTML UNI code字符,例如&放大器;俱乐部;
来自前pression
I am triying to output simples html unicode characters, for example ♣
from an expression.
我曾尝试使用 NG-绑定-HTML
和 NG-结合HTML的不安全
,但我可以'吨让它出现在我的HTML。
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 ='和;俱乐部;';
<跨度NG绑定-HTML ={{字符}}>< / SPAN>
- 的HTML code未注入跨度应答器
- 的HTML code不是PTED(的HTML字符不会出现在我的控制台间$ P $,但如果我不从控制器使用前pression并直接调用
<跨度NG绑定-HTML =&放大器;俱乐部;>< / SPAN>
,我可以看到HTML字符正确间在我的控制台PTED $ p $,但还没有注入跨度应答器)
- The html code is not injected into the span balise
- 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="♣"></span>
, i can see the HTML character correctly interpreted in my console, but still not injected into the span balise)
如何从一个前pression输出HTML字符?
How to output html characters from an expression ?
我输入脚本 // ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-sanitize.min.js
并添加<$在我的应用程序C $ C> ngSanitize 依赖。
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.
推荐答案
您将不得不使用 $ SCE(严格的语境转义),ngHtmlBindUnsafe 1.2辗转
You will have to use $sce (Strict Contextual Escaping), ngHtmlBindUnsafe was removed in 1.2
function myCtrl($scope,$sce){
$scope.html = $sce.trustAsHtml('♣');
}
小提琴: http://jsfiddle.net/TheSharpieOne/uPw2U/
Furthernore,你可以创建一个过滤器,这样你就不必逃避控制器的一切。
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>
小提琴: http://jsfiddle.net/TheSharpieOne/uPw2U/1/
这篇关于如何从一个前pression输出HTML UNI code字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!