[Fixed] How can I use a filter inside a filter argument?

Issue

I have a date and I want to apply a format depends on the language so I want to get the string that contains the format and use it as a parameter for the date formatting. But it says me that the expression hasn’t a valid syntax, how can do that ?

Syntax Error: Token '{' invalid key at column 46 of the expression [resultsDate | date:{{'date.format' | translate] starting at [{'date.format' | translate].

{{ resultsDate | date:{{'date.format' | translate }}  }}

Solution

I am not sure if 'date.format' | translate is the right way to adjust the localization of the date format. In fact, the Angular date pipe accepts a locale parameter. See here to apply the locale parameter.

That said, you could wrap the first pipe in a <ng-container> with a *ngIf and leverage it’s as construct to "create" a local variable. The <ng-container> tags wouldn’t contribute to additional elements and would be commented out in the rendered DOM.

Try the following

<ng-container *ngIf="(date.format | translate) as translatedFormat">
  {{ resultsDate | date:translatedFormat }}
</ng-container>

Leave a Reply

(*) Required, Your email will not be published