首页 文章

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

提问于
浏览
2

我正在使用Angular 2.0.0-RC.4构建一个Angular 2管道 . 它应该基于属性栏过滤作为Foo数组传入的所有Foo对象 . 当我逐步调试调试器中的代码时,会填充'allFoo'var,就像'bar'var一样 . 我可以断点并在控制台中运行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 回答

  • 2

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

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

相关问题