为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 components.ts 文件中使用 KeyValuePipe?

如何在 components.ts 文件中使用 KeyValuePipe?

扬帆大鱼 2021-11-04 17:51:38
是否可以在 ts 文件中而不是在 html 中使用 KeyValuePipe?如果是的话,有人可以告诉我怎么做吗?这就是我现在拥有的,但显然我不能像这样使用管道。let item of this.entityDetails | keyvalue{            let properties:PropertyBase<any>[]=[                new TextboxProperty({                    key: item.key,                    label: item.key,                    value: item.value,                    required: false,                }),                ];        }
查看完整描述

3 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

你当然可以。你只需要像任何服务一样导入它并注入它。你从@angular/common.


import { KeyValuePipe } from "@angular/common";


constructor(private keyValuePipe: KeyValuePipe) {


    const transformed = this.keyValuePipe.transform(this.entityDetails);


    for (let item of transformed) {

      let properties: PropertyBase<any>[] = [

        new TextboxProperty({

          key: item.key,

          label: item.key,

          value: item.value,

          required: false

        })

      ];

    }

  }


查看完整回答
反对 回复 2021-11-04
?
慕斯王

TA贡献1864条经验 获得超2个赞

是的,您可以在 ts 文件中使用管道。


 constructor(public dataformatpipe: DateFormatPipe){}


ngOnInt(){}


  formatData(date) {

    return this.dataformatpipe.transform(date);

   }

html文件


 <span class="value-data">{{formatData(item.startData)}}</span>


查看完整回答
反对 回复 2021-11-04
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

在那里编写一个自定义管道,您可以在 ts 文件中写入,以便您可以转换,从父级作为管道输入传递并使用您的管道 ts 文件以供参考,请访问此处 https://alligator.io/angular/custom-pipes-angular/


查看完整回答
反对 回复 2021-11-04
  • 3 回答
  • 0 关注
  • 300 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信