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

Angularjs ng-repeat of string variable with special characters

我正在对其中包含字符串的数组执行 ng-repeat。问题是某些字符串具有特殊字符,例如 "degrees" 和小数。下面数组中第一项的输出是 "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

为了使特殊字符起作用,您需要使用 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"
            ];
}

查看 fiddle here