0

Hi I have a similar problem posted in here: Angular 2 Pipe under condition

But instead of creating custom pipes, I would like to use the built in ones.

Say I have an array like this:

[{value: '1', type:'number'},{value:'1/18/2018', type: 'date'}]

and would like to use it like this assuming I'm looping through it:

{{ arr.type ? (arr.value | arr.type) : (arr.value)}}

It gives me an error of Parser Error: Missing expected )

Dekso
  • 541
  • 4
  • 23
  • What if you do `| [arr.type]`? If that also doesn't work you can always use the pipes in the definition itself by injecting the pipe into constructor of the class in which the definition exists. – Chrillewoodz Jun 20 '18 at 07:52
  • arr,type ? (arr.type === 'number' ? (arr.value | number) : (arr.value | date) ) : arr.value – Muhammed Albarmavi Jun 20 '18 at 07:57
  • @Chrillewoodz it didnt work. For the latter solution I dont know if I understand you correctly but what I did is imported the pipes to my class then defined it in constructor and made my array like this. [{value: '1', type:this.decimalPipe},{value:'1/18/2018', type: this.datePipe}] but still did not work. I still get template errors. – Dekso Jun 20 '18 at 08:11
  • @MuhammadAlbarmawi I've also thought about that solution but what if I have to use more built in pipes, then I would have a long ternary statements. – Dekso Jun 20 '18 at 08:14

1 Answers1

1

You can do it like this:

public myArr = [this.numberPipe(1), this.datePipe('1/18/2018', 'yyyy-MM-dd')];

constructor(private numberPipe: NumberPipe, private datePipe: DatePipe) {}

And then in template simply loop the values and display it with {{arr}}.

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
  • Actually I am using primeng table here https://primefaces.org/primeng/#/table/sections do you know how I will implement it using primeng? – Dekso Jun 20 '18 at 08:25
  • I mean in the link I mentioned, myArr is only the equivalent of the cols array. It only contains the column headers and field names not the actual values which is what is illustrated in your example. – Dekso Jun 20 '18 at 08:36
  • That's outside of the scope of your question. Your current question is about how to use the pipes to get the correct values. If you need further help I suggest opening a new question that targets the other issue. – Chrillewoodz Jun 20 '18 at 08:38
  • I've opened a new question here https://stackoverflow.com/questions/50944629/angular-primeng-table-set-up-pipe-per-column-using-cols-array. Will you help me out? Thanks. – Dekso Jun 20 '18 at 09:23