加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:10:25 所属栏目:安全 来源:网络整理
导读:我很喜欢输出simples html unicode字符,例如 club;从一个表达。 我试图使用ng-bind-html和ng-bind-html-unsafe,但我不能使它出现在我的HTML。 $ scope.character =’ clubs;’; span ng-bind-html =“{{character}}” / span html代码不会注入到跨度打包中
我很喜欢输出simples html unicode字符,例如& club;从一个表达。

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

$ scope.character =’& clubs;’;

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

> html代码不会注入到跨度打包中
> html代码不被解释(html字符不出现在我的控制台中,但是如果我不使用控制器中的表达式,并直接调用< span ng-bind-html =“& clubs;”> ;< / span>,我可以看到在我的控制台中正确解释的HTML字符,但仍然没有注入到跨度中)

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

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

您将不得不使用 $sce (Strict Contextual Escaping),ngHtmlBindUnsafe在1.2中被删除
function myCtrl($scope,$sce){
    $scope.html = $sce.trustAsHtml('&clubs;');
}

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

此外,您可以创建一个过滤器,以便您无需逃避控制器中的所有内容。

.filter('html',function($sce){
    return function(input){
        return $sce.trustAsHtml(input);
    }
})

HTML:

<span ng-bind-html="'&clubs;'|html"></span>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读