使用AngularJS在HTML中显示Unicode? [英] Display Unicode in HTML with AngularJS?

查看:74
本文介绍了使用AngularJS在HTML中显示Unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用AngularJS控制器在HTML中显示Unicode时遇到问题。这是我的JavaScript:

I am having a problem displaying Unicode in HTML from an AngularJS controller. Here is my JavaScript:

var mOverview = angular.module('mOverview', []);

mOverview.controller('mOverviewController', function ($scope, $sce) {
  $scope.mData = [
    {
        'name': '↓'+'NASDAQ', // here the unicode is ↓ but the output is also ↓ which is supposed to be a down-arrow
        'amount': '15,698.85',
        'upDown': '+105.84'
        }
    ];
});

这是我的HTML:

<div ng-app="mOverview">
  <div ng-controller="mOverviewController">
    <table>
      <tr>
        <th></th>
        <th></th>
        <th></th>
      </tr>
      <tr ng-repeat="md in mData">
        <td>{{md.name}}</td>
        <td>{{md.amount}}</td>
        <td>{{md.upDown}}</td>
      </tr>
    </table>
 </div>

我试过 $ sanitize() trustAsHtml 但没有成功。那么,如何在我的网站上显示Unicode 向下箭头 HTML?

I tried $sanitise() and trustAsHtml but without success. So, how can I display the Unicode Downwards Arrow in my HTML?

推荐答案

角船与严格的上下文转义。因此,您必须明确表示要将某些字符呈现为HTML。

Angular ships with Strict Contextual Escaping. So you'd have to explicitly say that you want to render some character as HTML.

注意,我还在外部资源中添加了 ng-sanitize

Notice, I have also added ng-sanitize in the External Resources.

我创建了一个新过滤器

mOverview.filter('unsafe', function($sce) {
    return function(val) {
        return $sce.trustAsHtml(val);
    };
});

并在视图中使用它:

<td ng-bind-html="md.name | unsafe"></td>

http://jsfiddle.net/9UdVY/

这篇关于使用AngularJS在HTML中显示Unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆