Angularjs模板默认值,如果绑定Null / Undefined(带过滤器) [英] Angularjs Template Default Value if Binding Null / Undefined (With Filter)

查看:429
本文介绍了Angularjs模板默认值,如果绑定Null / Undefined(带过滤器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板绑定,使用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>

到目前为止一切顺利。但是,目前,如果日期字段中没有值,则绑定不显示任何内容。但是,如果没有日期,我希望它显示字符串'Various'。

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模板默认值,如果绑定Null / Undefined(带过滤器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆