Angular JS - ng-repeat 和循环条件 [英] Angular JS - ng-repeat and loop conditions

查看:40
本文介绍了Angular JS - ng-repeat 和循环条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有这段代码:

{{一世}}

它有效,但我想在循环中添加额外的条件,例如:

{{一世}}如果(我!== 0){<div></div>}

我怎样才能做到这一点?

解决方案

使用ng-if(或ng-show):

<div ng-if="i !== 0"></div>

附加到元素上,这将决定是否显示它.

可以在此处找到 ng-if 的文档.

但是,如果您只想在循环第一项或最后一项时执行某些操作,则可以使用 ng 的 $first$last 属性-repeat 也是如此.这些是记录在ng-repeat.并且可以这样使用:

<div ng-if="$first"></div>

Hey i have this piece of code:

<div ng-repeat="i in values">
{{i}}
</div>

It works but i would like to put extra conditions inside the loop something like:

<div ng-repeat="i in values">
{{i}}
if(i !== 0){
<div></div>
}
</div>

How can i achieve this?

解决方案

Use ng-if (or ng-show):

<div ng-repeat="i in values">
   <div ng-if="i !== 0"></div>
</div>

Attached to the element, this will decide whether to display it or not.

Documentation for ng-if can be found here.

However, if you want to only do something if you are looping the first or last item, you can use $first and $last properties of ng-repeat as well. These are documented with ng-repeat. And can be used like this:

<div ng-repeat="i in values">
   <div ng-if="$first"></div>
</div>

这篇关于Angular JS - ng-repeat 和循环条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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