【发布时间】:2018-12-19 19:23:11
【问题描述】:
您好,我正在尝试为我的 *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>
【问题讨论】:
-
传递给 filter() 的回调没有返回任何内容:return value.location.toLow... 此外,将布尔值与 true 进行比较是没有用的。只需返回布尔值。
-
谢谢你的回复,昨天我度过了漫长的一天,很沮丧:)