为什么这个 Angular 2 Pipe 没有返回数据?

Why is this Angular 2 Pipe not returning data?

我正在使用 Angular 2.0.0-RC.4 构建一个 Angular 2 管道。 它应该根据 属性 bar 过滤作为 Foo 数组传入的所有 Foo 对象。当我在调试器中单步执行代码时,'allFoo' 变量被填充,'bar' 变量也是如此。我可以断点和 运行 控制台中的 foos.filter() 代码,它 returns 一个我期望的数组。当我让函数完成时,它 returns 什么都没有。

我的代码有问题还是 Angular 2 RC4 中有错误?

这是打字稿:

import { Pipe, PipeTransform } from '@angular/core';
import { Foo } from '../foo';

@Pipe({
    name: 'barFilterPipe'
})

export class BarFilterPipe implements PipeTransform {
    transform(allFoo: Foo[], bar: string) {
        console.log(allFoo); //data appears correctly here
        console.debug(bar); //and correctly here
        if (allFoo) {
            if (bar == "" || bar == undefined) {
                return allFoo; //if user clears filter
            } else {
                let results: Foo[] = allFoo.filter(afoo => {
                    afoo.bars.indexOf(bar) !== -1; //breakpoint here and type this into console, it returns proper data
                });
                console.log(results); //nothing is returned here
                return results; // or here
            } 
        }
    }
}

供参考,每个 Foo 对象如下所示,其中条 属性 的数组中有不同的字符串:

{property1: -1, property2: "string", bars: ["A","B","C"]}

这是应用过滤器的模板文件:

<select [(ngModel)]="barFilter" class="form-control">
    <option *ngFor="let bar of barList" [value]="bar">{{bar}}</option>
</select>    
<table class="table">
            <thead>
                <tr>
                    <th>Property1 Name </th>
                    <th>Property2 Name</th>
                    <th>Bars</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let item of Foos | barFilterPipe : barFilter" [foofoo]="item"></tr>
            </tbody>
        </table>

这是加载该模板并使用过滤器的 Angular class:

import {Component, OnInit} from '@angular/core';
import {Foo} from '../foo';
import { BarFilterPipe } from '../Pipes/BarFilterPipe';

@Component({
    selector: 'my-component',
    templateUrl: '../components/myComponent.html',
    directives: [foofoo],
    pipes: [BarFilterPipe]
})
export class MyComponent implements OnInit {
    private barFilter: string;
    private barList: string[];
    private Foos: Foo[] = [{property1: -1, property2: "a", bars: ["A"]}, {property1: -1, property2: "z", bars: ["A","D"]}];
    constructor(){}
    ngOnInit(){
        this.barList = ["","A","B","C","D","E"];
    }
}

这是 Foo 模板和 class:

<td>
    <h2>{{thisFoo.property1}}</h2>
</td>
<td>
    {{thisFoo.property2}} &nbsp;
</td>
<td>
    <label *ngFor="let bar of thisFoo.bars">{{bar}} &nbsp;</label>
</td>

import {Component, Input} from '@angular/core';
import {Foo} from './foo';

@Component({
    selector: '[foofoo]',
    templateUrl: '../components/foofooComponent.html',
})

export class EstabSummary {
    @Input('foofoo') thisFoo;

}

如果我在 console.log(results); 行断点,我可以在控制台上键入以下内容并输出适当的数据:

let results = allFoo.filter(afoo => {afoo.bars.indexOf(bar);})

我可以 post 转换后的 JS,如果这有助于解决这个问题。

谢谢!

您的 filter 函数没有 return 包含数组元素的布尔值。我怀疑应该是:return afoo.bars.indexOf(bar) !== -1;

就像现在一样,每个元素都被排除了。