javascript AngularJS - 在绑定中使用三元运算符和过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26423245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - Using ternary operators and filters within a binding
提问by Oam Psy
I current have a simple data binding:
我目前有一个简单的数据绑定:
{{ myAccount.Balance }}
I think applied a couple of filters:
我认为应用了几个过滤器:
{{ myAccount.Balance | filter1 | filter2 }}
However, i want to use a ternary operator when the Balance is less than zero, the below works (without the filters):
但是,当余额小于零时,我想使用三元运算符,以下工作(没有过滤器):
{{ myAccount.Balance > 0 ? myAccount.Balance : myAccount.Balance + 'minus' }}
How can i use my filters 1 and 2 in the above too?
我怎样才能在上面使用我的过滤器 1 和 2?
回答by Rahil Wazir
You need to wrap them in parenthesis ()
to take precedency
您需要将它们用括号括起来()
以取得优先权
{{ (myAccount.Balance > 0 ? myAccount.Balance : myAccount.Balance + 'minus') | filter | filter 2 }}