AngularJS:html特殊字符未显示

时间:2022-08-24 17:03:13

I'm trying to display a string in my model that contains the html representation of a special character. Unfortunately, it doesn't show that actual character, and I don't know how to make it do it...

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

this is the code:

这是代码:

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

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

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

this is the output:

这是输出:

Hello, Superhero &lt;! 
<

here is a jsfiddle for that

这是一个jsfiddle

2 个解决方案

#1


6  

You can use ng-bind-html-unsafe directive:

您可以使用ng-bind-html-unsafe指令:

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

Check out the examples in the docs and the jsfiddle.

查看docs和jsfiddle中的示例。

#2


17  

I ran into this problem today. Using AngularJS, in data inside a controller, I was trying to include a string with 'n' with tilde. I tried "Español" as well as the html representation, and neither displayed the desired output.

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

A coworker helpfully pointed out that unicode works. So I tried

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

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

which gave me

这给了我

my site en Español

as desired. Check out http://unicode-table.com/en/

如预期的。查看http://unicode-table.com/en/

#1


6  

You can use ng-bind-html-unsafe directive:

您可以使用ng-bind-html-unsafe指令:

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

Check out the examples in the docs and the jsfiddle.

查看docs和jsfiddle中的示例。

#2


17  

I ran into this problem today. Using AngularJS, in data inside a controller, I was trying to include a string with 'n' with tilde. I tried "Español" as well as the html representation, and neither displayed the desired output.

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

A coworker helpfully pointed out that unicode works. So I tried

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

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

which gave me

这给了我

my site en Español

as desired. Check out http://unicode-table.com/en/

如预期的。查看http://unicode-table.com/en/