角度表达式:可以做类型安全"or"||



在我的AngularJS视图中,我喜欢使用这样的表达式

{{myScopeVariable || defaultText}}

但是,如果 myScopeVariable 为 "0",则会显示 defaultText。有没有办法计算表达式类型安全的第一部分?

我希望有以下行为:

if myScopeVariable is 'undefined'
--> defaultText is shown
if myScopeVariable is 2
---> myScopeVariable is shown
if myScopeVariable is 0
---> myScopeVariable is shown (currently not the case) 

感谢您的帮助!

就像 Phix 评论的那样,我会选择过滤器。

实际上,对于一些常见的过滤器,有一个很好的库,其中还包括一个默认过滤器:https://github.com/frapontillo/angular-filters

(下面是默认筛选器的代码:https://github.com/frapontillo/angular-filters/blob/develop/src/filters/default/default.js(

最新更新