因此,为了便于使用,我想添加复选框作为过滤器。但是,它仅在我刷新页面时加载。
这些是我的文件:
文件名.component.ts
import { Component } from '@angular/core';
import { CheckboxFilterPipe } from './checkbox-filter.pipe';
@Component({
    templateUrl: 'app/filename.component.html',
    pipes: [CheckboxFilterPipe]
})
export class filenameComponent {
    checkboxes: any[] = [{
        id: 1,
        label: 'Filter 1',
        state: true
    }, {
        id: 2,
        label: 'Filter 2',
        state: true
    }];
    displayData: any[] = [
        // Objects I want to display
    ];
}
复选框-filter.pipe.ts
import { Pipe, Pipetransform } from '@angular/core';
@Pipe({
    name: 'CheckboxFilter'
})
export class CheckboxFilterPipe implements PipeTransform {
    transform(values: any[], args: string[]): boolean {
        console.log(args);
        return values.filter(value=> {
            // My filtercode, return true for now
            return true;
        });
    }
}
文件名.component.html
<div class="content-wrapper">
    <div class="row">
        <label *ngFor="let cb of checkboxes">
            <input type="checkbox" [(ngModel)]="cb.state"> {{cb.label}}
        </label>
    </div>
</div>
<table>
    <tr *ngFor="let value of displayData | CheckboxFilter:checkboxes">
        <td>{{ value.value1 }}</td>
        <td>{{ value.value2 }}</td>
        <td>{{ value.value2 }}</td>
    </tr>
</table>
我正在使用Angular2.rc.0。为了清楚起见,我在这个问题中重命名了我的变量。控制台中的输出仅在我刷新页面时发生,而不是在我(取消)选中复选框时发生。任何解决此问题的帮助表示赞赏。