javascript - Angular 2. *ngFor 问题,当我使用 Pipe 时

标签 javascript arrays node.js angular javascript-objects

我有一个组件。

@Component({
    selector: 'top3',
    templateUrl: 'dev/templates/top3.html',
    pipes: [orderBy],
    providers: [HTTP_PROVIDERS, ParticipantService]
})
export class AppTop3Component implements OnInit {

    constructor (private _participantService: ParticipantService) {}

    errorMessage: string;
    participants: any[];

    ngOnInit() {
        this.getParticipants();
    }

    getParticipants() {
        this._participantService.getParticipants()
            .then(
                participants => this.participants = participants,
                error => this.errorMessage = <any>error
            );
    }

}

此组件使用名为 _participantService 的服务。 _participantService 检索对象数组。我在组件的模板中输出我的对象数组:

<h2>Top 3</h2>
<table class="table table-bordered table-condensed" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Score</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="#participant of participants | orderBy: '-score'; #i = index">
            <td>{{i+1}}</td>
            <td>{{participant.username}}</td>
            <td>{{participant.score}}</td>
        </tr>
    </tbody>
</table>

我使用一个名为 orderBy 的管道,带有 *ngFor 指令。问题是当我不以这种方式使用管道和输出数组时:

<tr *ngFor="#participant of participants; #i = index">

一切正常,我得到了正确的结果:

Correct result

但是当我想对我的对象数组进行排序并使用我的管道时,我没有任何输出:

Incorrect result

我的 Pipe 函数中有一个 undefined object ^

@Pipe({
    name: 'orderBy',
    pure: false
})

export class orderBy implements PipeTransform {
    transform(arr: any[], orderFields: string[]): any {
        console.log(arr);
        orderFields.forEach(function(currentField: string) {
            var orderType = 'ASC';

            if (currentField[0] === '-') {
                currentField = currentField.substring(1);
                orderType = 'DESC';
            }

            arr.sort(function(a, b) {
                return (orderType === 'ASC') ? 
                        (a[currentField] < b[currentField]) ? -1 :
                        (a[currentField] === b[currentField]) ? 0 : 1 :                                                                                                                   
                        (a[currentField] < b[currentField]) ? 1 : 
                        (a[currentField] === b[currentField]) ? 0 : -1;
            });

        });
        return arr;
    }
}

enter image description here

最佳答案

由于您使用 promise 异步加载数据,因此它们在开始时为空(在调用 then 方法中定义的第一个回调之前)。

您需要检查管道中的 are 参数是否为 null。如果是这样,你不应该执行“order by”处理......

当结果出现时,将使用数据再次调用管道(不会为空)。在这种情况下,您将能够对数据进行排序...

你可以试试这段代码:

@Pipe({
    name: 'orderBy',
    pure: false
})

export class orderBy implements PipeTransform {
    transform(arr: any[], orderFields: string[]): any {
      if (arr==null) {
        return null;
      }
      (...)
    }
}

关于javascript - Angular 2. *ngFor 问题,当我使用 Pipe 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35507024/

相关文章:

php - 减去数组值

Java读取文件并将文本存储为数组

javascript - 将缓冲区(图像)转换为文件

javascript - 如何在 Javascript 中设置打字动画的样式

javascript - 如何检查数组中的每一项?

javascript - 如何在 Node js中获取第二个数组?

node.js - Mongoosejs 虚拟填充

javascript - Node js接收多条消息参数undefined

javascript - 为什么我的拖动在 d3 中没有缩放?

javascript - 如何制作带有子链接的可点击列表?