【问题标题】:Angular ng-bind-html inside a nested ng-repeat嵌套的 ng-repeat 中的 Angular ng-bind-html
【发布时间】:2015-05-06 04:38:43
【问题描述】:

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

【问题讨论】:

  • 你能告诉我们item 中的内容吗?当你说它不起作用时,你的意思是你什么都没看到?

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


【解决方案1】:

使用$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>

【讨论】:

    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      相关资源
      最近更新 更多