MWPodgorni
MWPodgorni

Reputation: 83

How to use KeyValuePipe in components.ts file?

Is it possible to use KeyValuePipe not in html but in ts file? If yes, could someone show me how?

This is what I have now but obviously I cannot use the pipe like this.

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

Big thanks in advance

Upvotes: 0

Views: 1157

Answers (3)

upinder kumar
upinder kumar

Reputation: 837

yes You Can use pipe in ts file.

 constructor(public dataformatpipe: DateFormatPipe){}

ngOnInt(){}

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

html file

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

Upvotes: 0

korteee
korteee

Reputation: 2682

Sure you can. You just have to import it and inject it like any service. You import it from @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
        })
      ];
    }
  }

Upvotes: 2

shashi kumar
shashi kumar

Reputation: 382

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/

Upvotes: 0

Related Questions