Gnujeremie
Gnujeremie

Reputation: 594

Pipe with piped parameter in angular 2

I'd like to write something like

<p>{{"CURRENT_DATE" | translate:(value:(currentDate | date:getDateFormat))}}</p>

where translate is a pipe function from ng2-translate.

I'd like to display: "Today is 2016-07-13", so CURRENT_DATE is "Today is {{value}}" and expects a dynamic value.

Depending on the user's locale, the current date format changes. I have a function getDateFormat which returns "yy-MM-dd" or "dd/MM/yy".

I know it is possible to chain pipes, but my case here is not really chaining pipes.

Is there a simple way, or do I have to write a custom pipe ?

Thanks !

EDIT: Okay my bad, I was too dumb to copy the example without errors. I should have written :

<p>{{"CURRENT_DATE" | translate:{value:currentDate | date:getDateFormat } }}</p>

Upvotes: 4

Views: 1102

Answers (2)

Felix
Felix

Reputation: 4595

This works:

  • en.json

    'DETAIL': 'Liability {{id}} - from {{date}}'

  • template

    {{ 'DETAIL' | translate: { id: id, date: lastModifiedDate | date: 'medium' } }}

Upvotes: 1

James Emmrich
James Emmrich

Reputation: 51

Why not do something like:

<p>{{"CURRENT_DATE" | translate:{value: getDate()}}}</p>

And then getDate() function creates the date in the locale required using a combination of the information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Upvotes: 2

Related Questions