Pipe with piped parameter in angular 2

故事扮演 提交于 2019-12-10 18:11:43

问题


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>

回答1:


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




回答2:


This works:

  • en.json

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

  • template

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



来源:https://stackoverflow.com/questions/38355577/pipe-with-piped-parameter-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!