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

时间:2016-09-11 23:15:27

标签: javascript angular typescript angular2-pipe

我正在使用Angular 2.0.0-RC.4构建一个Angular 2管道。 它应该基于属性栏过滤作为Foo数组传入的所有Foo对象。当我逐步调试调试器中的代码时,' allFoo' var填充了,' bar'变种。我可以断点并在控制台中运行foos.filter()代码,它返回一个我期望的数组。当我让函数完成时,它什么都不返回。

我的代码是否有问题,或者Angular 2 RC4中有错误?

这是TypeScript:

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对象都是这样的,其中bars属性在其数组中将有不同的字符串:

{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类:

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模板和类:

<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);})

我可以发布已编译的JS,如果这有助于解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:2)

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

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