如果绑定为空/未定义(带过滤器),Angularjs 模板默认值 [英] Angularjs Template Default Value if Binding Null / Undefined (With Filter)
问题描述
我有一个模板绑定,它使用 Angular 的日期过滤器显示一个名为date"的模型属性,它是一个日期.
I have a template binding that displays a model attribute called 'date' which is a date, using Angular's date filter.
<span class="gallery-date">{{gallery.date | date:'mediumDate'}}</span>
到目前为止一切顺利.但是目前,如果日期字段中没有值,则绑定将不显示任何内容.但是,如果没有日期,我希望它显示字符串各种".
So far so good. However at the moment, if there is no value in the date field, the binding displays nothing. However, I would like it to display the string 'Various' if there is no date.
我可以使用二元运算符获得基本逻辑:
I can get the basic logic using a binary operator:
<span class="gallery-date">{{gallery.date || 'Various'}}</span>
但是我不能让它与日期过滤器一起工作:
However I can't get it to work with the date filter:
<span class="gallery-date">{{gallery.date | date:'mediumDate' || "Various"}}</span>
如何在日期过滤器旁边使用二元运算符?
推荐答案
原来我需要做的就是将表达式的左侧用软括号括起来:
Turns out all I needed to do was wrap the left-hand side of the expression in soft brackets:
<span class="gallery-date">{{(gallery.date | date:'mediumDate') || "Various"}}</span>
这篇关于如果绑定为空/未定义(带过滤器),Angularjs 模板默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!