0

This is a follow-up question to Angular2 - assign pipe from a variable

What I'm looking for is a way to use a pipe based on a variable name. I tried what Günter suggested and created a pipe that returns other pipes, but how would you return another pipe and make sure it's not rendered as text?

I have the following pipe:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'piper'
})
export class PiperPipe implements PipeTransform {
  transform(value: any, args?: any): any {
    return `{{${value} | ${args}}}`;
  }
}

But when I feed it with a date string and "date" argument, like this:

<!-- with e.g. obj = { value: "2016-11-08T11:11:40.000Z", pipe: "date" } -->
<div>{{obj.value | obj.pipe}}</div>

It renders it as innerText:

<div>{{2016-11-08T11:11:40.000Z | date}}</div>

I tried [innerHTML] but no luck either.

br.julien
  • 3,420
  • 2
  • 23
  • 44
Nicholas
  • 21
  • 3
  • try this piper:obj.value | piper:obj.date – rashfmnb Nov 30 '16 at 17:14
  • Possible duplicate of [how to create and call a pipe from the component in angular 2?](http://stackoverflow.com/questions/36913541/how-to-create-and-call-a-pipe-from-the-component-in-angular-2) – Maximilian Riegler Nov 30 '16 at 17:18
  • You seem to be painting yourself into a corner with this use case. Why not use `[ngSwitch]` or similar and have different interpolation strings? – Dan Wilson Nov 30 '16 at 17:56

0 Answers0