AngularJS:ng-show表示ng-repeat中$ index的最高值

时间:2022-11-25 17:19:15

I have a input form for email address and address.

我有一个电子邮件地址和地址的输入表格。

User can add upto 3 emails and address-lines dynamically by clicking + sign on the right of the input field.

用户可以通过单击输入字段右侧的+符号动态添加最多3封电子邮件和地址行。

When I ng-repeat input field + is also repeated for every input field. How to show that + for only the last input field where the value of $index is maximum.AngularJS:ng-show表示ng-repeat中$ index的最高值

当对每个输入字段重复输入字段+时,也会重复输入字段+。如何仅显示$ index的值最大的最后一个输入字段的+。

1 个解决方案

#1


16  

You can instead use $last special property of ng-repeat. It is a boolean value property set for the last item (item here means child scope created by ng-repeat and not the value of that particular iteration) in the ng-repeat. Something like:

您可以使用ng-repeat的$ last特殊属性。它是ng-repeat中最后一项(此处的项表示由ng-repeat创建的子范围而不是该特定迭代的值)的布尔值属性集。就像是:

  ... ng-show="$last"

or

 ... ng-if="$last"

See other special properties of ng-repeat

查看ng-repeat的其他特殊属性

  • $index - number iterator offset of the repeated element (0..length-1)
  • $ index - 重复元素的数字迭代器偏移量(0..length-1)

  • $first - boolean true if the repeated element is first in the iterator.
  • $ first - 如果重复元素是迭代器中的第一个元素,则返回true。

  • $middle - boolean true if the repeated element is between the first and last in the iterator.
  • $ middle - boolean如果重复元素位于迭代器中的第一个和最后一个元素之间,则为true。

  • $last - boolean true if the repeated element is last in the iterator.
  • $ last - boolean如果重复元素在迭代器中的最后一个,则为true。

  • $even - boolean true if the iterator position $index is even (otherwise false).
  • $ even - boolean如果迭代器位置$ index是偶数,则为true(否则为false)。

  • $odd - boolean true if the iterator position $index is odd (otherwise false).
  • $ odd - 布尔值如果迭代器位置$ index为奇数,则为true(否则为false)。

Also note that if you plan to use ng-show (not ng-if) you could as well achieve it using css selectors pseudo-class like :last-child , :last-of-type etc based on the context and DOM structure.

另请注意,如果您计划使用ng-show(非ng-if),您也可以使用css选择器伪类实现它,例如:last-child,:last-of-type等基于上下文和DOM结构。

#1


16  

You can instead use $last special property of ng-repeat. It is a boolean value property set for the last item (item here means child scope created by ng-repeat and not the value of that particular iteration) in the ng-repeat. Something like:

您可以使用ng-repeat的$ last特殊属性。它是ng-repeat中最后一项(此处的项表示由ng-repeat创建的子范围而不是该特定迭代的值)的布尔值属性集。就像是:

  ... ng-show="$last"

or

 ... ng-if="$last"

See other special properties of ng-repeat

查看ng-repeat的其他特殊属性

  • $index - number iterator offset of the repeated element (0..length-1)
  • $ index - 重复元素的数字迭代器偏移量(0..length-1)

  • $first - boolean true if the repeated element is first in the iterator.
  • $ first - 如果重复元素是迭代器中的第一个元素,则返回true。

  • $middle - boolean true if the repeated element is between the first and last in the iterator.
  • $ middle - boolean如果重复元素位于迭代器中的第一个和最后一个元素之间,则为true。

  • $last - boolean true if the repeated element is last in the iterator.
  • $ last - boolean如果重复元素在迭代器中的最后一个,则为true。

  • $even - boolean true if the iterator position $index is even (otherwise false).
  • $ even - boolean如果迭代器位置$ index是偶数,则为true(否则为false)。

  • $odd - boolean true if the iterator position $index is odd (otherwise false).
  • $ odd - 布尔值如果迭代器位置$ index为奇数,则为true(否则为false)。

Also note that if you plan to use ng-show (not ng-if) you could as well achieve it using css selectors pseudo-class like :last-child , :last-of-type etc based on the context and DOM structure.

另请注意,如果您计划使用ng-show(非ng-if),您也可以使用css选择器伪类实现它,例如:last-child,:last-of-type等基于上下文和DOM结构。