具有动态arg的货币管道

问题描述 投票:0回答:3

这个 :

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency }}
</div>

有效,但默认情况下是美元。

docs(https://angular.io/api/common/CurrencyPipe)说你可以通过的第一个arg是ISO 4217标准中的货币

我有ISO 4217标准的货币,但它必须是动态的。它不是每个组件,也不是每个表,它是表中的每个条目。

Angular currency pipe拒绝解释并且某种程度上期望args是静态的:

这被读作一个字符串,并且在货币值之后写出的“getCurrency(row)”显示在我的页面上:

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency:'getCurrency(row)' }}
</div>

这是一个HTML语法错误:

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency:'{{row['currency']}}' }}
</div>

这是这样的:

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency:'{row['currency']}' }}
</div>

这是这样的:

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency:{row['currency']} }}
</div>

这是这样的:

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency:'row['currency']' }}
</div>

有没有办法解决这个问题? (不,我不打算为现有的每种货币加上*ngIf="")。

angular dynamic formatting pipe currency
3个回答
1
投票

在您的每个玩具中,您将值解释为单引号

例子

{{ row[col] | currency:'getCurrency(row)' }}

如果你的方法getCurrency()返回一个ISO 4217字符串,只需删除那些quot和angular将正确解释它们

{{ row[col] | currency:getCurrency(row)}}


1
投票

试试这个:

<div *ngIf="isMonetary(col)">
  {{ row[col] | currency:row['currency'] }}
</div>

1
投票

我用纯粹的暴力想出来了

它的 :

{{ row[col] | currency:(row['currency']) }}
© www.soinside.com 2019 - 2024. All rights reserved.