gpt4 book ai didi

javascript - 使用 AngularJS 在 HTML 中显示 Unicode?

转载 作者:太空狗 更新时间:2023-10-29 13:33:37 25 4
gpt4 key购买 nike

我在从 AngularJS Controller 以 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>

我尝试了 $sanitise()trustAsHtml 但没有成功。那么,如何显示 Unicode Downwards Arrow在我的 HTML 中?

最佳答案

避免从脚本编写 HTML 标记。一旦数据可能包含 HTML 特殊字符,如 <&你有破损和潜在的安全问题 (XSS)。

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

'name': '\u2193NASDAQ',

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

'name': '↓NASDAQ',

关于javascript - 使用 AngularJS 在 HTML 中显示 Unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com