加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

角管用于去除双打

发布时间:2020-12-17 17:34:48 所属栏目:安全 来源:网络整理
导读:我有下拉菜单,列出了客户的所有国家/地区代码.目前我遇到的问题是我有很多双重条目,即如果我有三个来自印度的客户,我的下拉列表将显示IN,IN,IN而不是一次.我以为我可以在管道的帮助下解决这个问题,但是我知道如何实现它. 下面是我的管道(它是空的,因为我无法
我有下拉菜单,列出了客户的所有国家/地区代码.目前我遇到的问题是我有很多双重条目,即如果我有三个来自印度的客户,我的下拉列表将显示IN,IN,IN而不是一次.我以为我可以在管道的帮助下解决这个问题,但是我知道如何实现它.

下面是我的管道(它是空的,因为我无法弄清楚代码):

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

@Pipe({
  name: 'duplicates'
})
export class DuplicatesPipe implements PipeTransform {

  transform(value: any,args?: any): any {
    
    return value;


  }

}

在这里我的html下拉:

<strong class="ml-2">Country</strong>
     <select class="ml-1" name="countryCode" [(ngModel)]="countryCode" (change)="gender = ''" (change)="activeStatus = ''">
  <option></option>
  <option *ngFor="let customer of customerArray">{{customer.countryCode | duplicates}}</option>
 </select>

解决方法

您必须将管道应用于您正在迭代的数组.

例如这样的事情:

@Pipe({ name: "uniqueCountryCodes" })
export class UniqueCountryCodesPipe implements PipeTransform {

  transform(customers: Customer[],args?: any): any { 
    return customers.map(c => c.countryCode).filter((code,currentIndex,allCodes) => allCodes.indexOf(code) === currentIndex);
  }

}

用法:

<option *ngFor="let code of (customerArray | uniqueCountryCodes)">{{ code }}</option>

请记住,只要管道不是impure,它只过滤一次,并且在将新客户添加到customerArray时不会更新国家/地区代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读