在EX pression Angularjs的if-then-else的建设 [英] Angularjs if-then-else construction in expression

查看:208
本文介绍了在EX pression Angularjs的if-then-else的建设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以以某种方式使用IF-THEN-ELSE建设angularjs前pression,比如我有函数$ scope.isExists(项目)有返回bool值。
我想这样的事情,

Can I somehow use if-then-else construction in angularjs expression, for example I have function $scope.isExists(item) that has to return bool value. I want something like this,

<div ng-repeater="item in items">
    <div>{{item.description}}</div>
    <div>{{isExists(item) ? 'available' : 'oh no, you don't have it'}}</div>
</div>

我知道,我可以使用函数返回的字符串,我用的if-then-else的建设纳入前pression的可能性很有趣。
谢谢你。

I know that I can use function that returns string, I'm interesting in possibility of using if-then-else construction into expression. Thanks.

推荐答案

角前pressions不支持三元运算符1.1.5之前,但它可以效仿这样的:

Angular expressions do not support the ternary operator before 1.1.5, but it can be emulated like this:

condition && (answer if true) || (answer if false)

所以例如,像这样的工作:

So in example, something like this would work:

<div ng-repeater="item in items">
    <div>{{item.description}}</div>
    <div>{{isExists(item) && 'available' || 'oh no, you don't have it'}}</div>
</div>

更新:角1.1.5增加了对三元运营商的支持:

UPDATE: Angular 1.1.5 added support for ternary operators:

{{myVar === "two" ? "it's true" : "it's false"}}

这篇关于在EX pression Angularjs的if-then-else的建设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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