在EX pression Angularjs的if-then-else的建设

在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"}}