首页 > 解决方案 > 如何在打字稿中的箭头函数中传递参数

问题描述

我正在尝试实现自定义管道/

   transform(value: any, id?: number, field?:string): any {
       if(value)
       {
        return value.filter(contact=>contact.ContactType===id)
       }
      }

我像这样从html调用它

*ngFor="let mailingContact of facilityContacts | contactFilter:1:'ContactType'"

在这里,我将在 id 参数中获取值'1',并直接将其设置为箭头函数。但我也想用参数字段名称更改contact.ContactType中的ContactType。

有没有办法实现它?

标签: angularpipes-filters

解决方案


您可以使用[]属性访问运算符。

return value.filter(contact => contact[field] === id)

推荐阅读