{"id":313326,"date":"2024-05-20T23:01:30","date_gmt":"2024-05-20T15:01:30","guid":{"rendered":""},"modified":"2024-05-20T23:01:40","modified_gmt":"2024-05-20T15:01:40","slug":"%e6%80%8e%e4%b9%88%e5%9c%a8-angular6-%e4%b8%ad%e4%bd%bf%e7%94%a8-filter-%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e9%a1%b5%e9%9d%a2%e6%90%9c%e7%b4%a2%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/313326.html","title":{"rendered":"\u600e\u4e48\u5728 Angular6 \u4e2d\u4f7f\u7528 Filter \u5b9e\u73b0\u4e00\u4e2a\u9875\u9762\u641c\u7d22\u529f\u80fd"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234129\/f515aa03-1119-4349-a8c4-181f4df8665c.webp\" alt=\"\u600e\u4e48\u5728 Angular6 \u4e2d\u4f7f\u7528 Filter \u5b9e\u73b0\u4e00\u4e2a\u9875\u9762\u641c\u7d22\u529f\u80fd\" \/><\/p>\n<p><p><strong>\u5728Angular6\u4e2d\uff0c\u8981\u5b9e\u73b0\u9875\u9762\u641c\u7d22\u529f\u80fd\u53ef\u4ee5\u901a\u8fc7\u7ba1\u9053\uff08Pipe\uff09\u3001\u670d\u52a1\uff08Service\uff09\u548c\u7ec4\u4ef6\uff08Component\uff09\u534f\u540c\u5de5\u4f5c\u6765\u5b8c\u6210\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7ba1\u9053\u6765\u8fc7\u6ee4\u6570\u636e\uff0c\u7136\u540e\u5728\u7ec4\u4ef6\u4e2d\u901a\u8fc7\u4e00\u4e2a\u641c\u7d22\u8f93\u5165\u6846\u6765\u7ed1\u5b9a\u8f93\u5165\u503c\uff0c\u5e76\u901a\u8fc7\u670d\u52a1\u6765\u83b7\u53d6\u548c\u5904\u7406\u6570\u636e\uff0c\u6700\u540e\u5c06\u8fc7\u6ee4\u7ed3\u679c\u663e\u793a\u5728\u9875\u9762\u4e0a\u3002<\/strong><\/p>\n<\/p>\n<p><p>Angular \u4e0d\u518d\u5185\u7f6e\u8fc7\u6ee4\u5668\uff08filter\uff09\u529f\u80fd\uff0c\u8fd9\u662f\u56e0\u4e3a\u4eceAngularJS\u8f6c\u5230Angular\u6846\u67b6\u65f6\uff0c\u5f00\u53d1\u8005\u56e2\u961f\u53d1\u73b0\u5185\u7f6e\u8fc7\u6ee4\u5668\u53ef\u80fd\u4f1a\u5728\u6bcf\u4e2a\u68c0\u6d4b\u5468\u671f\u4e2d\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u5904\u7406\uff0c\u4ece\u800c\u5f71\u54cd\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u5728Angular\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u81ea\u5b9a\u4e49\u7ba1\u9053\u6765\u5b9e\u73b0\u8fc7\u6ee4\u5668\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u7ba1\u9053<\/strong><\/h3>\n<\/p>\n<p><p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7ba1\u9053\uff0c\u5b83\u5c06\u8d1f\u8d23\u6839\u636e\u7528\u6237\u7684\u8f93\u5165\u6267\u884c\u8fc7\u6ee4\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">import { Pipe, PipeTransform } from &#039;@angular\/core&#039;;<\/p>\n<p>@Pipe({<\/p>\n<p>  name: &#039;filter&#039;<\/p>\n<p>})<\/p>\n<p>export class FilterPipe implements PipeTransform {<\/p>\n<p>  transform(items: any[], searchTerm: string, filterField: string): any[] {<\/p>\n<p>    if (!items || !searchTerm) {<\/p>\n<p>      return items;<\/p>\n<p>    }<\/p>\n<p>    return items.filter(item =&gt; <\/p>\n<p>      item[filterField].toLowerCase().includes(searchTerm.toLowerCase())<\/p>\n<p>    );<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ba1\u9053\u5b9e\u73b0\u4e2d\uff0c<code>transform<\/code> \u65b9\u6cd5\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a<code>items<\/code> \u662f\u5f85\u8fc7\u6ee4\u7684\u6570\u636e\u6570\u7ec4\uff0c<code>searchTerm<\/code> \u662f\u7528\u4e8e\u641c\u7d22\u7684\u5173\u952e\u8bcd\uff0c<code>filterField<\/code> \u662f\u9700\u8981\u5728\u5176\u4e2d\u641c\u7d22\u7684\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u521b\u5efa\u641c\u7d22\u670d\u52a1<\/strong><\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6765\u7ba1\u7406\u8981\u663e\u793a\u7684\u6570\u636e\u4ee5\u53ca\u641c\u7d22\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">import { Injectable } from &#039;@angular\/core&#039;;<\/p>\n<p>import { BehaviorSubject } from &#039;rxjs&#039;;<\/p>\n<p>@Injectable({<\/p>\n<p>  providedIn: &#039;root&#039;<\/p>\n<p>})<\/p>\n<p>export class SearchService {<\/p>\n<p>  private dataSource = new BehaviorSubject&lt;any[]&gt;([]);<\/p>\n<p>  currentData = this.dataSource.asObservable();<\/p>\n<p>  constructor() {}<\/p>\n<p>  updateData(data: any[]) {<\/p>\n<p>    this.dataSource.next(data);<\/p>\n<p>  }<\/p>\n<p>  \/\/ \u5176\u4ed6\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5......<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u670d\u52a1\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>BehaviorSubject<\/code>\u6765\u4fdd\u5b58\u5f53\u524d\u7684\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u4e86<code>updateData<\/code>\u65b9\u6cd5\u6765\u66f4\u65b0\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u6784\u5efa\u641c\u7d22\u7ec4\u4ef6<\/strong><\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u8fd9\u4e2a\u7ec4\u4ef6\u5305\u542b\u4e00\u4e2a\u8f93\u5165\u6846\uff0c\u7528\u4e8e\u63a5\u6536\u7528\u6237\u7684\u641c\u7d22\u8f93\u5165\uff0c\u5e76\u80fd\u5c06\u7ed3\u679c\u5c55\u793a\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- search.component.html --&gt;<\/p>\n<p>&lt;div&gt;<\/p>\n<p>  &lt;input type=&quot;text&quot; [(ngModel)]=&quot;searchTerm&quot; placeholder=&quot;\u641c\u7d22...&quot;&gt;<\/p>\n<p>  &lt;ul&gt;<\/p>\n<p>    &lt;li *ngFor=&quot;let item of items | filter: searchTerm:&#039;name&#039;&quot;&gt;{{ item.name }}&lt;\/li&gt;<\/p>\n<p>  &lt;\/ul&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ec4\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Angular\u7684\u53cc\u5411\u6570\u636e\u7ed1\u5b9a\uff0c\u901a\u8fc7<code>ngModel<\/code>\u6765\u7ed1\u5b9a<code>searchTerm<\/code>\u53d8\u91cf\uff0c\u5b83\u5c06\u4f5c\u4e3a\u7528\u6237\u7684\u641c\u7d22\u8f93\u5165\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u81ea\u5b9a\u4e49\u7684<code>filter<\/code>\u7ba1\u9053\u6765\u8fc7\u6ee4<code>items<\/code>\u6570\u7ec4\uff0c\u5e76\u6309<code>name<\/code>\u5b57\u6bb5\u8fdb\u884c\u641c\u7d22\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">import { Component, OnInit } from &#039;@angular\/core&#039;;<\/p>\n<p>import { SearchService } from &#039;.\/search.service&#039;;<\/p>\n<p>@Component({<\/p>\n<p>  selector: &#039;app-search&#039;,<\/p>\n<p>  templateUrl: &#039;.\/search.component.html&#039;,<\/p>\n<p>})<\/p>\n<p>export class SearchComponent implements OnInit {<\/p>\n<p>  items: any[] = [];<\/p>\n<p>  searchTerm: string = &#039;&#039;;<\/p>\n<p>  constructor(private searchService: SearchService) {}<\/p>\n<p>  ngOnInit() {<\/p>\n<p>    this.searchService.currentData.subscribe(data =&gt; this.items = data);<\/p>\n<p>  }<\/p>\n<p>  \/\/ \u5176\u4ed6\u7ec4\u4ef6\u903b\u8f91......<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ec4\u4ef6\u7684\u7c7b\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u8ba2\u9605\u4e86<code>SearchService<\/code>\u670d\u52a1\u7684<code>currentData<\/code>\uff0c\u4ee5\u4fbf\u5728\u6570\u636e\u66f4\u65b0\u65f6\u91cd\u65b0\u83b7\u53d6\u8fc7\u6ee4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u96c6\u6210\u81f3\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4e0d\u8981\u5fd8\u8bb0\u5c06\u670d\u52a1\u3001\u7ba1\u9053\u548c\u7ec4\u4ef6\u6dfb\u52a0\u5230\u5e94\u7528\u7a0b\u5e8f\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">import { NgModule } from &#039;@angular\/core&#039;;<\/p>\n<p>import { FormsModule } from &#039;@angular\/forms&#039;;<\/p>\n<p>import { BrowserModule } from &#039;@angular\/platform-browser&#039;;<\/p>\n<p>import { FilterPipe } from &#039;.\/filter.pipe&#039;;<\/p>\n<p>import { SearchComponent } from &#039;.\/search.component&#039;;<\/p>\n<p>import { SearchService } from &#039;.\/search.service&#039;;<\/p>\n<p>@NgModule({<\/p>\n<p>  declarations: [<\/p>\n<p>    SearchComponent,<\/p>\n<p>    FilterPipe<\/p>\n<p>  ],<\/p>\n<p>  imports: [<\/p>\n<p>    BrowserModule,<\/p>\n<p>    FormsModule<\/p>\n<p>  ],<\/p>\n<p>  providers: [SearchService],<\/p>\n<p>  bootstrap: [SearchComponent]<\/p>\n<p>})<\/p>\n<p>export class Ap<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/52627.html\" target=\"_blank\">PMO<\/a>dule { }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>AppModule<\/code>\u4e2d\uff0c\u6211\u4eec\u5c06<code>FormsModule<\/code>\u6dfb\u52a0\u5230\u5bfc\u5165\u4e2d\uff0c\u4ee5\u4fbf\u80fd\u591f\u4f7f\u7528<code>ngModel<\/code>\u3002\u540c\u65f6\u6ce8\u518c\u4e86<code>FilterPipe<\/code>\u548c<code>SearchService<\/code>\u670d\u52a1\uff0c\u786e\u4fdd\u5b83\u4eec\u53ef\u4ee5\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Angular6\u5e94\u7528\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4e00\u4e2a\u6709\u6548\u7684\u9875\u9762\u641c\u7d22\u529f\u80fd\u3002\u6b64\u529f\u80fd\u4e0d\u4ec5\u5feb\u901f\u4e14\u9ad8\u6548\uff0c\u8fd8\u53ef\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u5b9a\u5236\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728 Angular6 \u4e2d\u5982\u4f55\u4f7f\u7528 Filter \u5b9e\u73b0\u9875\u9762\u641c\u7d22\u529f\u80fd\uff1f<\/strong><\/p>\n<p>\u5728 Angular6 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 Pipe \u548c\u81ea\u5b9a\u4e49 Filter \u65b9\u6cd5\u6765\u5b9e\u73b0\u9875\u9762\u641c\u7d22\u529f\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>ngFor<\/code> \u6307\u4ee4\u5728 HTML \u4e2d\u904d\u5386\u8981\u641c\u7d22\u7684\u6570\u636e\u5217\u8868\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>ngModel<\/code> \u6307\u4ee4\u6765\u7ed1\u5b9a\u4e00\u4e2a\u8f93\u5165\u6846\uff0c\u4ee5\u4fbf\u7528\u6237\u8f93\u5165\u641c\u7d22\u5173\u952e\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 Pipe\uff0c\u5728\u5176\u4e2d\u5b9e\u73b0\u8fc7\u6ee4\u903b\u8f91\u3002\u6700\u540e\uff0c\u5c06\u81ea\u5b9a\u4e49\u7684 Pipe \u5e94\u7528\u5230 ngFor \u6307\u4ee4\u7684\u5faa\u73af\u4e2d\uff0c\u4ee5\u4fbf\u6839\u636e\u7528\u6237\u8f93\u5165\u52a8\u6001\u8fc7\u6ee4\u6570\u636e\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728 Angular6 \u4e2d\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49 Filter\uff1f<\/strong><\/p>\n<p>\u5728 Angular6 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>@Pipe<\/code> \u88c5\u9970\u5668\u6765\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 Filter\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>ng generate pipe<\/code> \u547d\u4ee4\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u6a21\u677f\u3002\u7136\u540e\uff0c\u5728\u81ea\u5b9a\u4e49\u7684 Filter \u7c7b\u4e2d\uff0c\u5b9e\u73b0 <code>transform<\/code> \u65b9\u6cd5\u6765\u5b9a\u4e49\u8fc7\u6ee4\u903b\u8f91\u3002<code>transform<\/code> \u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u8fc7\u6ee4\u7684\u6570\u636e\u548c\u7528\u6237\u8f93\u5165\u7684\u5173\u952e\u5b57\u3002\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 Array \u65b9\u6cd5\uff08\u5982 <code>filter<\/code>\u3001<code>includes<\/code>\u7b49\uff09\u6765\u5b9e\u73b0\u8fc7\u6ee4\u903b\u8f91\u3002\u6700\u540e\uff0c\u5c06\u81ea\u5b9a\u4e49\u7684 Filter \u7c7b\u5bfc\u51fa\u5e76\u5728\u9700\u8981\u4f7f\u7528\u7684\u7ec4\u4ef6\u4e2d\u5f15\u5165\uff0c\u5e76\u901a\u8fc7\u7ba1\u9053\u8bed\u6cd5\u5c06\u5176\u5e94\u7528\u5230 ngFor \u6307\u4ee4\u7684\u5faa\u73af\u4e2d\u3002<\/p>\n<p><strong>3. \u9664\u4e86\u4f7f\u7528 Filter\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728 Angular6 \u4e2d\u5b9e\u73b0\u9875\u9762\u641c\u7d22\u529f\u80fd\u5417\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0c\u5728 Angular6 \u4e2d\u9664\u4e86\u4f7f\u7528 Filter\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u9875\u9762\u641c\u7d22\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 Reactive Forms \u6765\u521b\u5efa\u4e00\u4e2a\u8868\u5355\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u641c\u7d22\u5b57\u6bb5\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 RxJS \u7684 <code>debounceTime<\/code> \u548c <code>distinctUntilChanged<\/code> \u64cd\u4f5c\u7b26\u6765\u76d1\u542c\u8be5\u5b57\u6bb5\u7684\u53d8\u5316\uff0c\u5e76\u89e6\u53d1\u641c\u7d22\u64cd\u4f5c\u3002\u5728\u641c\u7d22\u64cd\u4f5c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 Array \u65b9\u6cd5\uff08\u5982 <code>filter<\/code>\u3001<code>includes<\/code>\u7b49\uff09\u6765\u5b9e\u73b0\u6570\u636e\u7684\u52a8\u6001\u8fc7\u6ee4\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u641c\u7d22\u903b\u8f91\uff0c\u5e76\u4e14\u80fd\u591f\u5b9e\u65f6\u54cd\u5e94\u7528\u6237\u7684\u8f93\u5165\u53d8\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Angular6\u4e2d\uff0c\u8981\u5b9e\u73b0\u9875\u9762\u641c\u7d22\u529f\u80fd\u53ef\u4ee5\u901a\u8fc7\u7ba1\u9053\uff08Pipe\uff09\u3001\u670d\u52a1\uff08Service\uff09\u548c\u7ec4\u4ef6\uff08Compone [&hellip;]","protected":false},"author":3,"featured_media":313338,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/313326"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=313326"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/313326\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/313338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=313326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=313326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=313326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}