javascript Angular.js 上的 ng-repeat 指令中的 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14739511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:27:57  来源:igfitidea点击:

if statement within a ng-repeat directive on Angular.js

javascripthtmlangularjs

提问by marceloduende

I am trying to implement an if into a ng-repeat directive but I am having a hard time. my code which work for now is:

我正在尝试将 if 实现到 ng-repeat 指令中,但我遇到了困难。我现在工作的代码是:

<p ng-repeat="list in lists">{{list[id].title}}</p>

What I want to do is basically

我想做的基本上是

<p ng-repeat="list in lists if list[id].selected">{{list[id].title}}</p>

Of course, on the second line I am getting an error. Any advice on this?

当然,在第二行我收到一个错误。对此有何建议?

Thank you.

谢谢你。

回答by Jan.J

As I wrote in a comment, you could use filters to achieve that. Here's example: http://jsfiddle.net/sebmade/ZfGx4/44/

正如我在评论中所写,您可以使用过滤器来实现这一点。这是示例:http: //jsfiddle.net/sebmade/ZfGx4/44/

ng-repeat="list in lists | filter:myFilter"


And filter code:


和过滤代码:

$scope.myFilter = function(item) {
    return item.selected == true;
};


Edit:
I found that it is possible to do it with inline filter like this:


编辑:
我发现可以像这样使用内联过滤器来做到这一点:

ng-repeat="list in lists | filter:{selected: true}"

回答by Shai Reznik - HiRez.io

What you need to add here is a filter:

您需要在此处添加一个过滤器:

<p ng-repeat="list in lists | filter:{selected:true}">test {{list.title}}</p>

I've added a plnkr as an example.

我添加了一个 plnkr 作为示例