具有特殊字符的字符串变量的Angularjs ng-repeat

时间:2015-02-23 21:26:24

标签: angularjs character ng-repeat

我正在对包含字符串的数组进行ng-repeat。问题是一些字符串有特殊字符,如“度”和小数。下面数组中第一项的输出是“Oven 350&#176”....而不是“Oven 350 *”。

directions = [
				"Oven 350°",
				"Cook potatoes in boiling salted water until done",
				"The next day grate potatoes coarsely",
				"Mix with remaining ingredients",
				"Place in shallow 1.5 or 2 quart baking dish and bake about 35 minutes"
			];
<ul>
  <li ng-repeat="item in directions"></li>
</ul

1 个答案:

答案 0 :(得分:3)

为了让特殊字符发挥作用,您需要使用ngBindHtml Documentation here

要使用ngBindHtml,您需要包含angular-sanitize.min.js并在模块依赖项中包含ngSanitize

<强> HTML

<div ng-controller="MyCtrl">
  <ul>
    <li ng-repeat="item in directions">
      <span ng-bind-html='item'></span>
    </li>
  </ul>
</div>

<强>的JavaScript

var app = angular.module('myApp', ['ngSanitize']);

function MyCtrl($scope) {
    $scope.directions = [
                "Oven 350&#176;",
                "Cook potatoes in boiling salted water until done",
                "The next day grate potatoes coarsely",
                "Mix with remaining ingredients",
                "Place in shallow 1.5 or 2 quart baking dish and bake about 35 minutes"
            ];
}

查看小提琴here