所以我在ng-repeat中有一个ng-repeat。内部ng-repeat引用了recipe.ingredients"中的"项。问题是每个"项目"有特殊字符,除非我使用ng-bind-html,否则不会渲染。但是,当我尝试使用ng-bind-html时,它不起作用。这是html:
这有效,但没有正确显示特殊字符(成分测量的分数):
<div class="row" ng-repeat="recipe in recipes">
<h1 class='title'> {{ recipe.title }} </h1>
<div class="col-md-5">
<div class="list-group">
<div class="list-title">Ingredients</div>
<p class="list-group-item" ng-repeat="item in recipe.ingredients">{{item}}</p>
</div>
</div>
</div>
我尝试使用ng-bind-html(这不起作用):
<div class="row" ng-repeat="recipe in recipes">
<h1 class='title'> {{ recipe.title }} </h1>
<div class="col-md-5">
<div class="list-group">
<div class="list-title">Ingredients</div>
<p class="list-group-item" ng-repeat="item in recipe.ingredients" ng-bind-html="item"></p>
</div>
</div>
</div>
&#34;项目的示例&#34;在ng-repeat中:
{
id: 1,
img: "images/beefThumbnail.jpg",
title: "Potatoes Supreme",
servings: "Servings: 8 - 10",
ingredients: [
"6 medium potatoes, peeled",
"Salt",
"½ cup butter or margarine, melted",
"2 cups shredded Cheddar cheese",
"⅓ cup chopped green onion",
"1 pint sour cream",
"¼ teaspoon pepper",
"½ teaspoon salt"
],
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"
]
},//end potatoesSupreme
答案 0 :(得分:5)
使用$ sce,也不要忘记将其注入控制器
JS
$scope.trustAsHtml = $sce.trustAsHtml
然后在HTML中
<div class="row" ng-repeat="recipe in recipes">
<h1 class='title'> {{ recipe.title }} </h1>
<div class="col-md-5">
<div class="list-group">
<div class="list-title">Ingredients</div>
<p class="list-group-item" ng-repeat="item in recipe.ingredients" ng-bind-html="trustAsHtml(item)"></p>
</div>
</div>
</div>
答案 1 :(得分:2)
如果您不想为每个控制器执行此操作,也可以使用过滤器。
JS
myApp.filter('trustAsHtml',['$sce', function($sce) {
return function(text) {
return $sce.trustAsHtml(text);
};
}]);
HTML
<p class="list-group-item" ng-repeat="item in recipe.ingredients" ng-bind-html="item | trustAsHtml"></p>