角使用JavaScript进行排序管道 [英] Angular using javascript to make sort Pipe

查看:80
本文介绍了角使用JavaScript进行排序管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular2中建立排序管道时遇到问题 这是我的管道代码:

I have a question when I build sort pipe in Angular2 here is my pipe code:

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

@Pipe({
  name: 'sort'
})
export class SortPipe implements PipeTransform {

  transform(value: any, propName: string): any {

    return value.sort((a,b)=> {
      return a[propName]-b[propName];
      // if(a[propName]>b[propName]) {
      //   return 1;
      // } else {
      //   return -1;
      // }
    });        
  }      
}

当我在注释中使用代码时,管道起作用,但是当我使用return [propName]-b[propName];时,管道不起作用;

When I use the code in comments, the pipe works, but when I use return a [propName]-b[propName]; it is not working;

推荐答案

要进行排序,必须返回一个整数(

For sort to work, you must return an integer (see reference). Are you sure, that subtracting your properties will always return these values?

这篇关于角使用JavaScript进行排序管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆