使用 AngularJS 在 HTML 中显示 Unicode?

2024-01-13

我在从 AngularJS 控制器显示 HTML 中的 Unicode 时遇到问题。这是我的 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>

I tried $sanitise() and trustAsHtml但没有成功。那么,如何显示 Unicode向下箭头 https://www.fileformat.info/info/unicode/char/2193/index.htm在我的 HTML 中?


避免从脚本编写 HTML 标记。一旦数据可能包含 HTML 特殊字符,例如< and &您遇到了损坏和潜在的安全问题 (XSS)。

HTML 标记引用的字符&darr;是 U+2193 向下箭头。您可以使用 JS 字符串转义直接在 JavaScript 中引用它:

'name': '\u2193NASDAQ',

或者,如果您的页面/脚本始终以 Unicode 安全格式(例如 UTF-8)保存和提供,那么您根本不需要转义它:

'name': '↓NASDAQ',
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 AngularJS 在 HTML 中显示 Unicode? 的相关文章

随机推荐