是否可以在没有实际金额/值的情况下使用货币管道?
与https://angular.io/api/common/CurrencyPipe中的值一样使用
@Component({
selector: 'currency-pipe',
template: `<div>
<!--output 'CA$0.26'-->
<p>{{a | currency:'CAD'}}</p>
</div>`
})
export class CurrencyPipeComponent {
a: number = 0.259;
b: number = 1.3495;
}输出:CA$0.26
但是,我需要输入'CAD‘并输出'CA$’。这是不是有可能呢?
所需的行为:
@Component({
selector: 'currency-pipe',
template: `<div>
<p>{{currency | currency:'CAD'}}</p>
</div>`
})所需输出:CA$
发布于 2021-10-22 12:52:48
如果你看一下源代码:
https://github.com/angular/angular/blob/master/packages/common/src/pipes/number_pipe.ts#L205
你会发现如果输入“不是一个值”,那么管道返回null --如果下面的函数返回false:
function isValue(value: number|string|null|undefined): value is number|string {
return !(value == null || value === '' || value !== value);
}也许,实现所需结果的最好方法是创建自己的管道。看一下CurrencyPipe的代码。最重要的片段是:
currency = getCurrencySymbol(currency, display === 'symbol' ? 'wide' : 'narrow', locale);这可能就是您想要使用的。
https://stackoverflow.com/questions/69676967
复制相似问题