gpt4 book ai didi

javascript - 在简写 if 语句中使用 Angular Pipe

转载 作者:行者123 更新时间:2023-12-02 23:30:20 24 4
gpt4 key购买 nike

如何在简写 if 语句中使用自定义管道?

这是我尝试使用的代码

<h1>Total Value {{data ? totalValue | moneyFormat : '$0'}}

这是我遇到的错误

Template parse errors:
Parser Error: Conditional expression data ? totalValue | moneyFormat : null requires all 3 expressions at the end of the expression

所以基本上我已经建立了一个格式化货币的管道,这是我的管道

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'moneyFormat'})
export class MoneyFormatPipe implements PipeTransform {
transform(value: number) {
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
let formattedValue = formatter.format(value);
formattedValue = formattedValue.slice(0, -3);
return formattedValue;
}
}

现在我知道这是有效的,因为我在其他场景中使用过它,比如

<p>This is your dollar amount {{dollarAmount | formatMoney}}

我错过了什么吗?我这样做错了吗?

请记住,我所做的只是为了简单起见而提供的示例,而不是条件语句的真正功能。我完全知道我可以使用管道返回 0 美元。

最佳答案

我认为你需要将某些部分括在括号中:

{{data ? (totalValue | moneyFormat) : '$0'}

关于javascript - 在简写 if 语句中使用 Angular Pipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56535795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com