簡體   English   中英

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

[英]How to use KeyValuePipe in components.ts file?

是否可以不在 html 但在 ts 文件中使用 KeyValuePipe? 如果是的話,有人可以告訴我怎么做嗎?

這就是我現在所擁有的,但顯然我不能像這樣使用 pipe。

let item of this.entityDetails | keyvalue{
            let properties:PropertyBase<any>[]=[
                new TextboxProperty({
                    key: item.key,
                    label: item.key,
                    value: item.value,
                    required: false,
                }),
                ];
        }

非常感謝提前

你當然可以。 你只需要像任何服務一樣導入它並注入它。 您從@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
        })
      ];
    }
  }

Write a custom pipe there you can write in ts file so you can transform, pass from parent as pipe input and work with your pipe ts file for refernce go here https://alligator.io/angular/custom-pipes-angular/

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

 constructor(public dataformatpipe: DateFormatPipe){}

ngOnInt(){}

  formatData(date) {
    return this.dataformatpipe.transform(date);
   }

html文件

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM