简体   繁体   English

角度4 * ng用于带有管道的数组长度

[英]Angular 4 *ngFor array length with pipe

I have a range slider in my app. 我的应用程序中有一个范围滑块。 If you drag the range slider, based on "sliderPipe" change results will appear. 如果拖动范围滑块,将基于“ sliderPipe”显示更改结果。 My problem is I'm unable to display the "No Records Found" message. 我的问题是我无法显示“找不到记录”消息。

html template html模板

<div class="well" *ngFor="let data of onewayFormData$ | 
                                  sliderPipe: sliderValue as results;">
  <div class="row">
    <div class="col-md-6 onewayresults">
      <h2> {{ data.cost | currency: 'INR':true }} </h2>
      <h3> {{ data.agencycode }} </h3>
      <h5> {{ data.ocity }} > {{ data.dcity }}</h5>
      <h5> Depart: {{ data.depart }} </h5>
      <h5> Arrive: {{ data.arrive }} </h5>
    </div>
    <div class="col-md-6">
      <div style="float:right;">
        <img [src]="data.imageurl" width="120px" height="100px" [alt]="data.agency" />
        <br/>
        <br/>
        <button class="btn btn-success"> Book Tickets </button>
      </div>
    </div>
  </div>
  Length: {{ results }}
  <div class="well" *ngIf="results.length == ''">
    <div class="alert alert-danger"> No Results Found....... </div>
  </div>
</div>

Pipe Code 管道编号

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'sliderPipe'
})
export class SliderPipePipe implements PipeTransform {
  transform(value, args ? ) { // ES6 array destructuring
  console.log('Arguments: ' , value.length);
  return value.filter(data => {
      return data.cost >= args;
    });
  }
}
If results is an array: 如果results是一个数组:

The length of an array is always of type number , so you need to check against results.length == 0 for an empty array. 数组的长度始终为number类型,因此您需要检查results.length == 0是否为空数组。

If results is of type number: 如果results是数字类型:

Check against results == 0 . 检查results == 0

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

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