简体   繁体   English

检查角度为5的数组长度* ng如果不显示任何内容

[英]Check array length in angular 5 *ngIf displays nothing

I am doing an *ngif to check if fitered results length is not 0 display items else display a template error. 我正在执行* ngif,以检查拟合结果的长度是否不为0,显示项目是否显示模板错误。

Unfortunately, the templateref works only when the filtered results has items. 不幸的是,templateref仅在过滤的结果包含项目时才起作用。

when it doesn't, the result.length displays nothing. 如果没有,则result.length不显示任何内容。

<div *ngFor="let collection of collections | async | filterBy: ['title']: search as result">
       <ng-container *ngIf='result?.length != 0; 
           then resultsIs else noresults'>
       </ng-container>

        <ng-template #resultsIs>
          <a routerLink="/collections/c/{{collection.title | 
            slugify}}/{{collection.id}}" 
               class="list-group-item list-group-item-action flex-column align-items-start"
          >
        <div class="d-flex w-100 justify-content-between">
          <h5 class="mb-1 text-primary"><u>{{collection.title}}</u></h5>
           <small>{{collection.updatedAt}}</small>
           </div>
           <p class="text-muted" >{{collection.description}}</p>
           <small>{{collection.username}}</small>
      </a>
    </ng-template>

    <ng-template #noresults>
     <div class="alert alert-info">
    <strong>No Match Found!</strong>
     Widen your search.</div>
       </ng-template>
     </div>

How can I check correctly if this object array length is less than zero so I can display this part of template 如何正确检查此对象数组的长度是否小于零,以便可以显示模板的这一部分

 <ng-template #noresults>
        <div class="alert alert-info"><strong>
          No Match Found!</strong> Widen your search.
         </div>
</ng-template>

The *ngIf for your template is inside a ngFor, which loops through a filtered list. 模板的* ngIf位于ngFor内,该ngFor遍历过滤列表。

I'm no sure exactly how you implemented your filter pipe, but I guess it will return an empty set if the list of collections does not match the filter criterion. 我不确定您是如何实现过滤器管道的,但是我猜想如果集合列表与过滤器标准不匹配,它将返回一个空集。

In that case, as normal behaviour, the ngFor will not render any element, so everything inside will never be evaluated by angular 在这种情况下,ngFor不会像常规行为那样渲染任何元素,因此其中的所有内容都不会通过angular求值。

What you'd need is to specify a default template if there is no element in the list provided to ngFor. 如果提供给ngFor的列表中没有元素,那么您需要指定一个默认模板。 This is not supported yet, but by looking at the recent commits for this request, this might be available soon 目前尚不支持此功能,但是通过查看此请求的最近提交,可能很快就会提供

https://github.com/angular/angular/issues/14479 https://github.com/angular/angular/issues/14479

Meanwile, you can do something like this 意思是,你可以做这样的事情

<div *ngIf='collections | async | filterBy: ['title']: search'; let filteredCollections>


    <div *ngFor="let collection of filteredCollections">


              <a routerLink="/collections/c/{{collection.title | 
                slugify}}/{{collection.id}}" 
                   class="list-group-item list-group-item-action flex-column align-items-start"
              >
            <div class="d-flex w-100 justify-content-between">
              <h5 class="mb-1 text-primary"><u>{{collection.title}}</u></h5>
               <small>{{collection.updatedAt}}</small>
               </div>
               <p class="text-muted" >{{collection.description}}</p>
               <small>{{collection.username}}</small>
          </a>


     </div>



    <div class="alert alert-info" *ngIf='filteredCollections.length === 0'>
        <strong>No Match Found!</strong>
        Widen your search.
    </div>

</div>

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

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