代码之家  ›  专栏  ›  技术社区  ›  A Coder

角6*ngfor

  •  0
  • A Coder  · 技术社区  · 8 年前

    我有两套代码,其中一套工作,另一套不工作。我需要知道为什么?

    不工作:

    <p *ngFor="let recp of recipes">
    <div class="row">
        <div class="col-xs-12">
            <a href="#" class="list-group-item clearfix">
                <div class="pull-left">
                    <h4 class="list-group-item-heading">{{recp.name}}</h4>
                    <p class="list-group-item-text">{{recp.description}}</p>
                </div>
                <span class="pull-right">
                    <img src="{{recp.imagePath}}" alt="" class="img-responsive" style="max-height:50px;">
                </span>
            </a>
            <app-recipe-item></app-recipe-item>
        </div>
    </div>
    

    错误:无法读取未定义的属性“name”。

    工作:

    <div class="row">
        <div class="col-xs-12">
            <a href="#" class="list-group-item clearfix" 
            *ngFor="let recp of recipes">
                <div class="pull-left">
                    <h4 class="list-group-item-heading">{{recp.name}}</h4>
                    <p class="list-group-item-text">{{recp.description}}</p>
                </div>
                <span class="pull-right">
                    <img src="{{recp.imagePath}}" alt="" class="img-responsive" style="max-height:50px;">
                </span>
            </a>
            <app-recipe-item></app-recipe-item>
        </div>
    </div>
    

    但如果你认为问题在于 <p> 标签,

    <p *ngFor ="let arr of addIncrement;>{{arr}}</p>
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Chellappan வ    8 年前

    根据MDN,在

    元素。Angular可能比你的浏览器更能执行这个规则

    p元素只允许包含内联元素

    查看此以获取更多信息:

    万维网联盟(W3C) https://www.w3.org/TR/html401/struct/text.html#h-9.3.1

    p元素表示一个段落。不能包含块级别 元素(包括p本身)。

    <!ELEMENT P - O (%inline;)*            -- paragraph -->
    
        2
  •  1
  •   David R    8 年前

    请检查一下 null (或) undefined 在插入之前。你可以使用 ? 在这种情况下,

    <h4 class="list-group-item-heading">{{recp?.name}}</h4>
       <p class="list-group-item-text">{{recp?.description}}</p>
    

    希望这有帮助!是的。

    推荐文章