首页 > 解决方案 > angular 8 Pipe 对数据进行排序但它不起作用

问题描述

我正在为在线考试创建一个角度应用程序。试图在其中使用管道。我正在通过我的数据服务进行一系列考试。我想根据受欢迎程度对它们进行排序,所以我创建了一个管道来完成这项工作。像这样..

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

@Pipe({
  name: 'popular'
})
export class PopularPipe implements PipeTransform {

  transform(value: any, args?: any): any {

    var popularTests =  value.filter(function(hero) {
      return hero.category ==  "Medical";
    });
    console.log(popularTests)// console is printing the array in the exact format i need 
   
  }

}

我的问题是当我在我的 html 中使用它时出现错误

can not read undefined

我是这项技术的新手,我按照文档中给出的步骤进行操作,但仍然没有运气,任何帮助都会有所帮助

标签: javascriptangulartypescript

解决方案


我认为应该有一个退货声明。因为要使其正常工作,您需要返回数据。您的管道工作正常,因为您正在通过控制台在管道中打印数据。最后添加return语句


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

@Pipe({
  name: 'popular'
})
export class PopularPipe implements PipeTransform {

  transform(value: any, args?: any): any {

    var popularTests =  value.filter(function(hero) {
      return hero.category ==  "Medical";
    });
    console.log(popularTests)
    return popularTests;// this will do the job
   
  }

}

如果这仍然给您任何错误,请粘贴您的 html


推荐阅读