angularjs - 嵌套的 ng-repeat 中的 Angular ng-bind-html

标签 angularjs angularjs-ng-repeat ng-bind-html

所以我在 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>

ng-repeat 中的“项目”示例:

       {
            id: 1,
            img: "images/beefThumbnail.jpg",
            title: "Potatoes Supreme",
            servings: "Servings: 8 - 10",
            ingredients: [
                "6 medium potatoes, peeled",
                "Salt",
                "&frac12; cup butter or margarine, melted",
                "2 cups shredded Cheddar cheese",
                "&frac13; cup chopped green onion",
                "1 pint sour cream",
                "&frac14; teaspoon pepper",
                "&frac12; teaspoon salt"
            ],
            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"
            ]
        },//end potatoesSupreme

最佳答案

使用$sce,也不要忘记将它注入(inject) Controller

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>

关于angularjs - 嵌套的 ng-repeat 中的 Angular ng-bind-html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28867199/

相关文章:

javascript - 在 AngularJS 中使用带有下划线的 WordPress Post Meta 时出现未定义

javascript - Node.js 上的 angular.toJSON() 函数/方法实现

html - angular ng-options 指令最终选择了多个项目

javascript - Angular JS 使用与其他行不同的控件将行添加到 ng-repeat 表中

javascript - AngularJs ng-repeat of objects orderBy object-id in array

ng-bind-html中的AngularJS数据绑定(bind)?

javascript - 在 ng-bind-html 中渲染 Angular 代码

python - 如何在传输过程中保护 REST API 负载?

javascript - 如何在不同范围内更新模型数组中的项目,并使更改反射(reflect)到各处?

angularjs - 即使模型没有变化,如何强制 Angular 重新绑定(bind)/更新