简体   繁体   English

如何将自定义管道应用于ngFor迭代元素

[英]How to apply a custom pipe to ngFor iteration elements

I have a custom pipe to highlight text. 我有一个自定义管道来突出显示文本。 I want to apply this pipe to ngfor iteration elements. 我想将此管道应用于ngfor迭代元素。 How can I do that? 我怎样才能做到这一点?

Here is my code: 这是我的代码:

 import { PipeTransform, Pipe } from '@angular/core'; @Pipe({ name: 'highlight' }) export class HighlightPipe implements PipeTransform { transform(text: string, search): string { if (search && text) { let pattern = search.toString().replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\\^\\$\\|]/g, '\\\\$&'); pattern = pattern.split(' ').filter((t) => { return t.length > 0; }).join('|'); const regex = new RegExp(pattern, 'gi'); return text.toString().replace(regex, (match) => `<span class="search-highlight">${match}</span>`); } else { return text; } } } 
 :host ::ng-deep .search-highlight{ background-color: #F2E366; } 
  <tbody> <tr *ngFor="let item of (itemsList | highlight)"> <td><i [ngClass]="{'fa fa-paperclip':item.ext === 'pdf'}"></i></td> <td>{{item.name}}<span [innerHTML]="text | highlight: searchTerm"></span></td> <td>{{item.path}}</td> <td>{{item.dateModification | date:'short':'':'fr'}}</td> </tr> </tbody> 

After @ChellappanV comments, specially this one , I changed my code in the search.component.html as following: @ChellappanV意见后,特别是这一个 ,我改变了我的代码在search.component.html如下:

<td>{{item.name}}<span [innerHTML]="item.name | highlight: searchTerm.value"></span></td>

instead of: 代替:

<td>{{item.name}}<span [innerHTML]="text | highlight: searchTerm"></span></td>

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

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