angular.js 中的内联条件

编程入门 行业动态 更新时间:2024-10-28 16:21:11
本文介绍了angular.js 中的内联条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想知道除了使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 08:10:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1019768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内联   条件   angular   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!