亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將過濾器應用于*ngFor?

如何將過濾器應用于*ngFor?

如何將過濾器應用于*ngFor?顯然,Range2將使用管道而不是過濾器(如Angular 1中的過濾器)與ng一起過濾結果,盡管實現似乎仍然是模糊的,沒有明確的文檔。也就是說,我想要達到的目標可以從以下的角度來看<div *ng-for="#item of itemsList" *ng-if="conditon(item)"></div>如何使用管道來實現?
查看完整描述

3 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

基本上,您可以編寫一個管道,然后在*ngFor指令。

在你的組成部分中:

filterargs = {title: 'hello'};items = [{title: 'hello world'}, {title: 'hello kitty'}, {title: 'foo bar'}];

在模板中,可以將字符串、數字或對象傳遞給管道以用于篩選:

<li *ngFor="let item of items | myfilter:filterargs">

在你的煙斗里:

import { Pipe, PipeTransform } from '@angular/core';@Pipe({
    name: 'myfilter',
    pure: false})export class MyFilterPipe implements PipeTransform {
    transform(items: any[], filter: Object): any {
        if (!items || !filter) {
            return items;
        }
        // filter items array, items which match and return true will be
        // kept, false will be filtered out
        return items.filter(item => item.title.indexOf(filter.title) !== -1);
    }}

記得把你的煙斗登記在app.module.ts;您不再需要在您的@Component

import { MyFilterPipe } from './shared/pipes/my-filter.pipe';@NgModule({
    imports: [
        ..
    ],
    declarations: [
        MyFilterPipe,
    ],
    providers: [
        ..
    ],
    bootstrap: [AppComponent]})export class AppModule { }

這是一個柱塞它演示了使用自定義過濾管和內置片管來限制結果。

請注意(正如幾位評論員所指出的)有一個原因為什么沒有內置的過濾管道的角度。


查看完整回答
反對 回復 2019-06-28
  • 3 回答
  • 0 關注
  • 1461 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號