简体   繁体   English

用角6制作过滤管

[英]Making filter pipe in angular6

I am trying to make a filter pipe in angular6. 我正在尝试用angular6做一个过滤管。 So far I got this: 到目前为止,我得到了:

search.component.html: search.component.html:

<input class="form-control" [(ngModel)]="searchService.query.title" name="title">

search.component.ts: search.component.ts:

import { Component, OnInit } from '@angular/core';
import { SearchDataService } from './search-data.service';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent {

  constructor(public searchService: SearchDataService) {
  }
}

search-data.service.ts: search-data.service.ts:

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SearchDataService {
  public query = {};
  constructor() { }
}

list.component.html: list.component.html:

<app-search></app-search>
<div>{{ searchService.query | json }}</div>
<div *ngIf="estates">
  <div *ngFor="let estate of estates | filter: searchService.query">
    <app-estates-estate [estate]="estate" [appClassSwitch]="'estate-details-list'"></app-estates-estate>
  </div>
</div>

If I change all this to just add a button to apply the filter (to assign the value from input to the service model) it works great. 如果我将所有这些更改为仅添加一个按钮以应用过滤器(将输入中的值分配给服务模型),则效果很好。 But if I do it the way it is shown here it does not refreshes the list with filters applied. 但是,如果按照此处显示的方式进行操作,则不会刷新应用了过滤器的列表。 I remember in angularjs that there were something like $rootScope.$digest so I tried to search something similar and I got to ApplicationRef and ChangeDetectorRef, but neither of these solved my case. 我记得在angularjs中有类似$ rootScope。$ digest的东西,所以我尝试搜索类似的东西,但我找到了ApplicationRef和ChangeDetectorRef,但是这些都不能解决我的问题。

So question is how can I enforce the filter application so that it affects the list shown on screen? 那么问题是如何执行过滤器应用程序,以使其影响屏幕上显示的列表?

Thanks 谢谢

Angular 2+ does not have a default filter like angularjs has. Angular 2+没有与angularjs一样的默认过滤器。 You should create your own filter(pipes in angular 2+). 您应该创建自己的过滤器(角度2+的管道)。 Please refer the below code snippet 请参考下面的代码片段

...
@pipe({
   name: "myCustomFilter",
   pure: false
})
export class MyCustomFilter implements PipeTransform {
   transform(input) {
      // Custom Logic 
      return output
    }
 }

Angular executes pure pipes only if any of the input changes... By default all pipes are pure... you should mark it as false... But this does comes with the cost of performance... please do take care 只有在任何输入更改的情况下,Angular才会执行纯管道...默认情况下,所有管道都是纯管道...您应将其标记为false ...但这确实会带来性能损失...请务必当心

...
@NgModule({
  imports: [CommonModule],
  declaration: [MyCustomFilter]
})
export class SomeModule {}

Then use in templates as follows 然后在模板中使用,如下所示

<div *ngFor="let item of items | myCustomFilter: 'searchQuery'"> 
   {{item.name}}
</div>

For More info about pipes please verify this Angular.io 有关管道的更多信息,请验证此Angular.io

You can refer this repo which has lots of reusable pipes angular-pipes 您可以参考此回购协议,其中有很多可重复使用的管道

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

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