angular.js 中的内联条件 [英] inline conditionals in angular.js

查看:32
本文介绍了angular.js 中的内联条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道除了使用 ng-show 等之外,是否有一种有条件的 angular 方式来有条件地显示内容.例如,在backbone.js 中,我可以对模板中的内联内容进行一些处理,例如:

I was wondering if there is a way in angular to conditionally display content other than using ng-show etc. For example in backbone.js I could do something with inline content in a template like:

<% if (myVar === "two") { %> show this<% } %>

但在 angular 中,我似乎仅限于显示和隐藏用 html 标签包装的东西

but in angular, I seem to be limited to showing and hiding things wrapped in html tags

<p ng-hide="true">I'm hidden</p>
<p ng-show="true">I'm shown</p>

在 angular 中使用 {{}} 有条件地显示和隐藏 angular 中的内联内容而不是将内容包装在 html 标签中的推荐方式是什么?

What is the recommended way in angular to conditionally show and hide inline content in angular just using {{}} rather than wrapping the content in html tags?

推荐答案

2Toad's answer below 就是你'重新找!给那个东西点赞

如果您使用的是 Angular <= 1.1.4,那么这个答案就可以:

对此还有一个答案.我发布了一个单独的答案,因为它更像是一个精确"的答案.尝试解决方案而不是可能的解决方案列表:

2Toad's answer below is what you're looking for! Upvote that thing

If you're using Angular <= 1.1.4 then this answer will do:

One more answer for this. I'm posting a separate answer, because it's more of an "exact" attempt at a solution than a list of possible solutions:

这是一个过滤器,它将执行立即如果"(又名 iif):

Here's a filter that will do an "immediate if" (aka iif):

app.filter('iif', function () {
   return function(input, trueValue, falseValue) {
        return input ? trueValue : falseValue;
   };
});

并且可以这样使用:

{{foo == "bar" | iif : "it's true" : "no, it's not"}}

这篇关于angular.js 中的内联条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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