javascript - 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
我是这项技术的新手,我按照文档中给出的步骤进行操作,但仍然没有运气,任何帮助都会有所帮助
解决方案
我认为应该有一个退货声明。因为要使其正常工作,您需要返回数据。您的管道工作正常,因为您正在通过控制台在管道中打印数据。最后添加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
推荐阅读
- system-verilog - 在 SystemVerilog 中制作二维数组/矩阵
- c# - 使用多个事实或单一理论
- java - Android:从 Firebase 数据库中获取带有两个 ValueEventListener 的数据(在 Arraylist Expires 之前设置适配器)
- scala - 简化嵌套函子转换
- c++ - 将完整的图片目录列表获取到不同的文件夹
- vue.js - 如何在Vue中再次从拦截器发送请求?
- reactjs - React Native:FlatList keyExtractor & toString() 问题?
- java - 使用 Camera2 API 的相机缩放设置
- android - 应用发明者 2 在填写之前清除列表
- spring-boot - 向新注册用户发送 gmail 消息时遇到问题