角管无法正常工作

时间:2018-07-11 16:58:52

标签: angular filter pipe

嗨,我正在尝试为* ngFor构建自定义管道,我想检查子字符串是否存在,如果存在,请打印它,否则不行。 这是我的代码,在此先感谢:

编辑,谢谢您的答复,这是我现在的解决方案:

    let secondDelimiterVal = this.getPosition(value.location,".",2);
    let secondDelimiterField = this.getPosition(field,".",2);
    let v = value.location.toLowerCase().substr(value.location.indexOf(".")+1,secondDelimiterVal-value.location.indexOf(".")-1);
    let needle = field.toLocaleLowerCase().substr(field.indexOf(".")+1,secondDelimiterField-field.indexOf(".")-1);      
    return v.includes(needle) == true;

我的烟斗:

import { Vulnerability } from '@models/api/vulnerability.model';
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'fullTextSearch',
  pure: false
})

export class FullTextSearchPipe implements PipeTransform {

  transform(values: Vulnerability[],field: string, include: boolean): any {
    if(include){

      return values.filter(value => {
        value.location.toLowerCase().substr(value.location.indexOf("."),value.location.lastIndexOf(".")).
        includes(field.toLocaleLowerCase().substr(field.indexOf("."),field.lastIndexOf("."))) === true;
      });
    } 
    else{
      return values.filter(value => value.location.toLowerCase().substr(value.location.indexOf("."),value.location.lastIndexOf(".")).search(field.toLocaleLowerCase().substr(field.indexOf("."),field.lastIndexOf("."))) == -1);
    } 
  }
}

我怎么称呼管道:

    <tr *ngFor="let finding of (verifiedFindings| fullTextSearch:job.packageName:true);index as i" > 
      <td>{{finding?.id}}</td>
      <td>{{finding?.title}}</td>
      <td>{{finding?.comment || finding?.location}}</td>
    </tr>    
...
        <tr *ngFor="let finding of newVerifiedElements | fullTextSearch:'job.packageName':false;index as i" class="table-info" > 
            <td>{{finding?.id}}</td>
            <td>{{finding?.title}}</td>
            <td>{{finding?.comment || finding?.location}}</td>
        </tr>  

1 个答案:

答案 0 :(得分:0)

谢谢你的提示,这让我走了

  return values.filter(value => {
    let secondDelimiterVal = this.getPosition(value.location,".",2);
    let secondDelimiterField = this.getPosition(field,".",2);
    let v = value.location.toLowerCase().substr(value.location.indexOf(".")+1,secondDelimiterVal-value.location.indexOf(".")-1);
    let needle = field.toLocaleLowerCase().substr(field.indexOf(".")+1,secondDelimiterField-field.indexOf(".")-1);      
    return v.includes(needle) == true;
    });