【问题标题】:AngularJS: html special characters are not showingAngularJS:没有显示html特殊字符
【发布时间】:2013-06-19 13:38:48
【问题描述】:

我试图在我的模型中显示一个字符串,其中包含一个特殊字符的 html 表示。不幸的是,它并没有显示出真正的角色,我不知道如何让它做到这一点......

这是代码:

<div ng-controller="MyCtrl">
  Hello, {{namea}}!
    <br/>
    &lt;
</div>

<script>
var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.namea = 'Superhero &lt;';
}
</script>

这是输出:

Hello, Superhero &lt;! 
<

here is a jsfiddle for that

【问题讨论】:

标签: angularjs


【解决方案1】:

我今天遇到了这个问题。使用 AngularJS,在控制器内的数据中,我试图包含一个带有 'n' 和波浪号的字符串。我尝试了“Español”以及 html 表示,但都没有显示所需的输出。

一位同事很有帮助地指出 unicode 有效。所以我尝试了

{
name : "my site en Espan\u00F1ol"
}

这给了我

my site en Español

根据需要。查看http://unicode-table.com/en/

【讨论】:

  • 我是 Angular 的新手,但我不明白的是,如果它位于已经采用 utf8 编码的页面上,为什么还会发生 html 编码? ñ 是该环境中的合法字符,甚至是拉丁文 1;它不是 &、 之类的 html 语法字符。它甚至不是像 ' 或 " 这样的“冒险”。那么,为什么 Angular 会与它混在一起,有没有办法让它停止?
  • 这是迄今为止更好的答案。非常感谢。
【解决方案2】:

你可以使用ng-bind-html-unsafe directive:

<div ng-controller="MyCtrl" ng-bind-html-unsafe="'Hello,' + namea">
</div>

查看examples in the docsjsfiddle

【讨论】:

  • 使用较新版本的 angular,ng-bind-html-unsafe 不再存在。您需要在 html 模板中使用 ng-bind-html 并在 js 函数中使用 $sce.trustAsHtml('Superhero <)
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多