Angular JS - ng-repeat 和循环条件 [英] Angular JS - ng-repeat and loop conditions
问题描述
嘿,我有这段代码:
{{一世}}
它有效,但我想在循环中添加额外的条件,例如:
{{一世}}如果(我!== 0){<div></div>}
我怎样才能做到这一点?
使用ng-if
(或ng-show
):
<div ng-if="i !== 0"></div>
附加到元素上,这将决定是否显示它.
可以在此处找到 ng-if 的文档.
但是,如果您只想在循环第一项或最后一项时执行某些操作,则可以使用 ng 的
也是如此.这些是记录在ng-repeat.并且可以这样使用:$first
和 $last
属性-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屋!