在angular.js内嵌条件语句 [英] inline conditionals in angular.js

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

问题描述

我在想,如果有一个方法的角度,以比采用NG-节目等。例如在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<% } %>

但棱角分明,我似乎仅限于显示和隐藏的东西包裹在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>

什么是角的推荐方式有条件地显示和隐藏在角内嵌的内容只是用{{}},而不是包装在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 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 seperate 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天全站免登陆