简体   繁体   English

如何将ngIf与ngFor结合起来

[英]How to combine ngIf with ngFor

I am new to Angular. 我是Angular的新手。

I would like to to print list of products, which are in relation with distributor. 我想打印与经销商有关的产品清单。 For that I thought about combaining *ngFor *ngIf so here is a code: 为此我想到了组合* ngFor * ngIf,所以这里是一个代码:

<div class="distributors-here ui-g-12" *ngIf="distributors">
  <h2 >Distributors  <i class="fa icon fa-plus"(click)="showDialog()"></i></h2>
  <p class="error-message" *ngIf="errorMessage.length >0">Operation couldn't be executed:</p>
  <p class="error-message">{{errorMessage}}</p>
  <ul class="distributors-list ui-g-12">
    <li class="distributor-item ui-md-3 ui-g-6" *ngFor="let distributor of distributors ">
      <i class="fa icon fa-times remove-distributor"(click)="removeDistributor(distributor.id,distributor.name)"></i>
      <div class="distributor-item-content">
        <p>id: {{distributor.id}}</p>
        <p>name: {{distributor.name}}</p>
          <div *ngIf="products">
            <p>products</p>
            <ol>
              <li  *ngFor="let product of products" *ngIf="product.distributor.id == distributor.id">dist</li>
            </ol>
          </div>
      </div>
    </li>
  </ul>
</div>

So I want to loop over the products array and if the distributor id of product matches with distributor id from other array I will have it printed new li element created. 所以我想遍历产品数组,如果产品的分销商ID与其他数组的分销商ID匹配,我将打印出新的li元素。

Unfortunately I get an error: 不幸的是我收到一个错误:

Can't have multiple template bindings on one element. 一个元素上不能有多个模板绑定。 Use only one attribute named 'template' or prefixed with * 仅使用一个名为“template”的属性或以*为前缀

[ERROR ->]*ngIf="product.distributor.id== distributor.id" [错误 - >] * ngIf =“product.distributor.id == distributor.id”

Does anyone knows how to refactor this code to make it work? 有谁知道如何重构此代码以使其工作?

Angular doesn't support more than one structural directive on the same element, so you should use the ng-container helper element here. Angular不支持同一元素上的多个结构指令,因此您应该在此处使用ng-container辅助元素。 FYI ng-container does not add an extra element to the DOM (the ul in Example 2 does get added to the DOM). FYI ng-container不会向DOM添加额外的元素(示例2中的ul确实会添加到DOM中)。

Example 1 例1

<ng-container *ngFor="let product of products" >
   <li *ngIf="product.distributor.id == distributor.id">{{ product?.name  }}</li>
</ng-container>

or 要么

Example 2 例2

<ul *ngFor="let product of products">
  <li *ngIf="product.distributor.id == distributor.id">
    {{log(thing)}}
    <span>{{thing.name}}</span>
  </li>
</ul>

You cannot use multiple structural directives on one html element. 您不能在一个html元素上使用多个结构指令。 For example you can instead place your *ngFor in a <ng-container> and wrap it around your <li> element. 例如,您可以将*ngFor放在<ng-container>并将其包裹在<li>元素周围。 The advantage of <ng-container> is the fact, that it doesn't introduce extra levels of HTML. <ng-container>的优点在于它不会引入额外的HTML级别。

 <ol> <ng-container *ngFor="let product of products"> <li *ngIf="product.distributor.id == distributor.id">dist</li> </ng-container> </ol> 

For more detail information have a look at the Official Angular Documentation (One structural directive per host element) 有关更多详细信息,请查看官方角度文档(每个主机元素一个结构指令)

You can use <ng-container> to have ngIf 您可以使用<ng-container>来获取ngIf

<div *ngIf="products">
   <p>products</p>
     <ol>
             <ng-container *ngFor="let product of products" >
             <li *ngIf="product.distributor.id == distributor.id">dist</li>
    </ol>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM